liveCleanup.test.mjs
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import test from 'node:test'
import { fileURLToPath, pathToFileURL } from 'node:url'
const modulePath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
'liveCleanup.mjs',
)
async function loadCleanup() {
assert.equal(fs.existsSync(modulePath), true, 'Live cleanup helper must exist')
return import(pathToFileURL(modulePath))
}
test('best-effort cleanup attempts every tracked order in reverse order', async () => {
const { cleanupTrackedOrders } = await loadCleanup()
const calls = []
const diagnostics = []
const client = {
async post(method, payload) {
calls.push({ method, orderCode: payload.orderCode })
if (method.endsWith('.revoke') && payload.orderCode === 'ORDER-2') {
throw new Error('fixture revoke failure')
}
if (method.endsWith('.revoke')) return { code: 0 }
return { code: 0, data: { status: 9 } }
},
}
const result = await cleanupTrackedOrders({
client,
context: { sessionId: 'runtime-session' },
orders: [
{ orderCode: 'ORDER-1', role: 'source' },
{ orderCode: 'ORDER-2', role: 'corrected' },
],
label: 'fixture',
onDiagnostic: (message) => diagnostics.push(message),
})
assert.deepEqual(
calls.filter((call) => call.method.endsWith('.revoke')).map((call) => call.orderCode),
['ORDER-2', 'ORDER-1'],
)
assert.equal(result.length, 2)
assert.equal(result.find((item) => item.orderCode === 'ORDER-2').revoked, false)
assert.equal(result.find((item) => item.orderCode === 'ORDER-1').revoked, true)
assert.equal(diagnostics.length, 1)
assert.doesNotMatch(JSON.stringify(result), /runtime-session/)
})