import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { createExecutorStateStore } from '../src/core/executor-state-store.mjs'; test('executor state store persists processing and submission state', () => { const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'unrip-executor-')); const store = createExecutorStateStore({ stateDir }); store.markProcessing('cmd-1', { quote_id: 'quote-1' }); assert.equal(store.get('cmd-1').status, 'processing'); store.markSubmitted('cmd-1', { result_event_id: 'result-1' }); assert.equal(store.get('cmd-1').status, 'submitted'); const reloaded = createExecutorStateStore({ stateDir }); assert.equal(reloaded.get('cmd-1').status, 'submitted'); assert.equal(reloaded.get('cmd-1').result_event_id, 'result-1'); }); test('executor state store normalizes legacy completed markers to submitted', () => { const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'unrip-executor-')); const statePath = path.join(stateDir, 'trade-executor-commands.json'); fs.writeFileSync( statePath, JSON.stringify({ commands: { 'cmd-legacy': { status: 'completed', result_event_id: 'result-legacy', }, }, }), ); const store = createExecutorStateStore({ stateDir }); assert.equal(store.get('cmd-legacy').status, 'submitted'); assert.equal(store.getState().commands['cmd-legacy'].status, 'submitted'); }); test('executor state store prunes old command records before serving state', () => { const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'unrip-executor-')); const statePath = path.join(stateDir, 'trade-executor-commands.json'); fs.writeFileSync( statePath, JSON.stringify({ commands: { 'cmd-old': { status: 'failed', updated_at: '2026-05-13T10:00:00.000Z', }, 'cmd-mid': { status: 'submitted', updated_at: '2026-05-13T10:01:00.000Z', }, 'cmd-new': { status: 'processing', updated_at: '2026-05-13T10:02:00.000Z', }, }, }), ); const store = createExecutorStateStore({ stateDir, maxCommands: 2 }); const state = store.getState(); assert.deepEqual(Object.keys(state.commands).sort(), ['cmd-mid', 'cmd-new']); assert.equal(state.evicted_count, 1); const summary = store.getSummary({ limit: 1 }); assert.equal(summary.total_commands, 2); assert.deepEqual(Object.keys(summary.commands), ['cmd-new']); assert.equal(summary.by_status.processing, 1); });