| 1 | import test from 'node:test' |
| 2 | import assert from 'node:assert/strict' |
| 3 | import { transformStockSnapshot } from '../dist/stock-adapters.mjs' |
| 4 | import { createHarnessModule } from '../dist/builtin/harness.mjs' |
| 5 | |
| 6 | const project = (operation, input) => ({ kind: 'stock_adapter', operation, input }) |
| 7 | const metadata = value => transformStockSnapshot(value).result.metadata |
| 8 | const owner = { plugin_id: 'host:harness', generation: 4, owner_token: 'opaque-fixture-owner' } |
| 9 | const params = extra => ({ owner, execution_id: 'captured-one', ticket: 'opaque-fixture-ticket', deadline_ms: 1000, ...extra }) |
| 10 | |
| 11 | const parser = value => ({ ok: true, value }) |
| 12 | const error = message => ({ ok: false, error: message }) |
| 13 | const data = extra => project('validate_data', { format: 'auto', source: 'inline', extension: null, json: error('JSON diagnostic'), toml: error('TOML diagnostic'), ...extra }) |
| 14 | test('data auto selects JSON then TOML, while a file extension pins its parser', () => { |
| 15 | assert.deepEqual(metadata(data({ json: parser({ kind: 'array', entries: 3 }), toml: parser({ kind: 'table', keys: ['a'] }) })), { valid: true, format: 'json', source: 'inline', summary: { top_level: 'array', entries: 3 } }) |
| 16 | assert.equal(metadata(data({ toml: parser({ kind: 'table', keys: ['a'] }) })).format, 'toml') |
| 17 | const pinned = transformStockSnapshot(data({ extension: 'json', toml: parser({ kind: 'table', keys: [] }) })).result |
| 18 | assert.equal(pinned.success, false); assert.equal(pinned.content, 'Invalid JSON: JSON diagnostic') |
| 19 | }) |
| 20 | test('data previews preserve Core captured ordering, ten-key cap and primitive types', () => { |
| 21 | const keys = ['😀', '\ue000', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '__proto__'] |
| 22 | for (const captured of [keys, keys.slice().sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b)))]) { |
| 23 | const summary = metadata(data({ json: parser({ kind: 'object', keys: captured }) })).summary |
| 24 | assert.deepEqual(summary.keys_preview, captured.slice(0, 10)) |
| 25 | assert.equal(summary.entries, 12) |
| 26 | } |
| 27 | for (const kind of ['string', 'number', 'boolean', 'null']) assert.equal(metadata(data({ json: parser({ kind }) })).summary.top_level, kind) |
| 28 | for (const kind of ['string', 'integer', 'float', 'boolean', 'datetime']) assert.equal(metadata(data({ format: 'toml', toml: parser({ kind }) })).summary.top_level, kind) |
| 29 | }) |
| 30 | test('parser diagnostics remain exact captured bytes and auto errors are unchanged', () => { |
| 31 | const result = transformStockSnapshot(data({ json: error('line 1:\n bad 😀'), toml: error('toml\nspan') })).result |
| 32 | assert.equal(result.content, 'Validation failed in auto mode: content is neither valid JSON nor TOML.') |
| 33 | assert.deepEqual(result.metadata, { valid: false, format: 'auto', source: 'inline', json_error: 'line 1:\n bad 😀', toml_error: 'toml\nspan' }) |
| 34 | }) |
| 35 | test('malformed, unknown and oversized snapshots refuse instead of producing results', () => { |
| 36 | for (const value of [null, {}, project('fetch', {}), project('finance_quote', {}), data({ json: parser({ kind: 'object', keys: ['a', 'a'] }) }), data({ json: parser({ kind: 'array', entries: -1 }) }), data({ source: 'x'.repeat(1024 * 1024) })]) assert.throws(() => transformStockSnapshot(value)) |
| 37 | }) |
| 38 | |
| 39 | test('actual pinned harness transforms one captured reply and sends only an opaque ticket', async () => { |
| 40 | const calls = [] |
| 41 | const runner = createHarnessModule({ async request(method, input) { calls.push([method, input]); return data({ json: parser({ kind: 'array', entries: 3 }) }) } }, owner) |
| 42 | const result = await runner.run(params(), new AbortController().signal) |
| 43 | assert.equal(result.result.metadata.valid, true) |
| 44 | assert.deepEqual(calls, [['exec/redeem', { owner, execution_id: 'captured-one', ticket: 'opaque-fixture-ticket' }]]) |
| 45 | await runner.dispose() |
| 46 | }) |
| 47 | test('wrong owner cannot redeem a stock operation and malformed reply has no fallback', async () => { |
| 48 | let calls = 0 |
| 49 | const runner = createHarnessModule({ async request() { calls++; return project('unknown', {}) } }, owner) |
| 50 | await assert.rejects(runner.run(params({ owner: { ...owner, plugin_id: 'native' } }), new AbortController().signal), /stale/) |
| 51 | assert.equal(calls, 0) |
| 52 | await assert.rejects(runner.run(params(), new AbortController().signal), /unadmitted/) |
| 53 | assert.equal(calls, 1); await runner.dispose() |
| 54 | }) |
| 55 | test('stock cancellation and owner disposal settle before a late captured reply', async () => { |
| 56 | let finish |
| 57 | const controller = new AbortController() |
| 58 | const runner = createHarnessModule({ request() { return new Promise(resolve => { finish = resolve }) } }, owner) |
| 59 | const pending = runner.run(params(), controller.signal) |
| 60 | controller.abort() |
| 61 | await assert.rejects(pending, /cancelled/) |
| 62 | finish(data({})); await runner.dispose() |
| 63 | await assert.rejects(runner.run(params(), new AbortController().signal), /stale/) |
| 64 | }) |
| 65 |