import test from 'node:test' import assert from 'node:assert/strict' import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { pathToFileURL } from 'node:url' import ts from 'typescript' async function importAuditNavigation() { const tempDir = await mkdtemp(join(tmpdir(), 'audit-navigation-')) const bundlePath = join(tempDir, 'auditNavigation.mjs') const source = await readFile('src/utils/business/auditNavigation.ts', 'utf8') const transpiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022 } }) await writeFile(bundlePath, transpiled.outputText, 'utf8') return { module: await import(pathToFileURL(bundlePath).href), cleanup: () => rm(tempDir, { recursive: true, force: true }) } } test('resource targets follow the platform, agent and enterprise stable-reference matrix', async () => { const { module, cleanup } = await importAuditNavigation() const { resolveAuditResourceTarget } = module const cases = [ { name: 'platform uses the internal resource id', input: { userType: 2, resourceType: 'iot_card', internalId: 8020, businessIdentifier: 'iccid' }, expected: { mode: 'resource', resourceType: 'iot_card', id: '8020' } }, { name: 'agent uses the trimmed subject identifier', input: { userType: 3, resourceType: 'iot_card', internalId: 8020, businessIdentifier: ' 8986112520903885262 ' }, expected: { mode: 'agent', resourceType: 'iot_card', id: '8986112520903885262' } }, { name: 'enterprise uses a device identifier', input: { userType: 4, resourceType: 'device', internalId: 9, businessIdentifier: ' SN-001 ' }, expected: { mode: 'enterprise', resourceType: 'device', id: 'SN-001' } }, { name: 'enterprise cannot use organization resources', input: { userType: 4, resourceType: 'shop', businessIdentifier: 'SHOP-1' }, expected: null }, { name: 'agent cannot infer an unsupported business resource', input: { userType: 3, resourceType: 'refund', businessIdentifier: 'RF-1' }, expected: null }, { name: 'platform hides a missing internal id', input: { userType: 2, resourceType: 'order', businessIdentifier: 'ORD-1' }, expected: null }, { name: 'platform hides zero ids', input: { userType: 1, resourceType: 'device', internalId: 0 }, expected: null }, { name: 'platform hides whitespace ids', input: { userType: 1, resourceType: 'device', internalId: ' ' }, expected: null } ] try { for (const scenario of cases) { assert.deepEqual(resolveAuditResourceTarget(scenario.input), scenario.expected, scenario.name) } } finally { await cleanup() } }) test('finance targets preserve only complete backend-provided conditions', async () => { const { module, cleanup } = await importAuditNavigation() const { resolveFinanceAuditTarget } = module const cases = [ ['numeric business id', 'order_id', 72, { mode: 'finance', field: 'order_id', value: 72 }], [ 'trimmed business number', 'refund_no', ' RF20260807171319801844 ', { mode: 'finance', field: 'refund_no', value: 'RF20260807171319801844' } ], ['missing value', 'wallet_id', undefined, null], ['zero value', 'payment_id', 0, null], ['whitespace value', 'correlation_id', ' ', null] ] try { for (const [name, field, value, expected] of cases) { assert.deepEqual(resolveFinanceAuditTarget(field, value), expected, name) } } finally { await cleanup() } }) test('explicit investigation references suppress empty, unreliable and self targets', async () => { const { module, cleanup } = await importAuditNavigation() const { resolveInvestigationReference } = module try { assert.equal(resolveInvestigationReference(' request-1 '), 'request-1') assert.equal(resolveInvestigationReference(' '), null) assert.equal(resolveInvestigationReference('event-1', { currentId: 'event-1' }), null) assert.equal(resolveInvestigationReference('event-2', { currentId: 'event-1' }), 'event-2') assert.equal(resolveInvestigationReference('correlation-1', { fidelity: false }), null) } finally { await cleanup() } })