/* * Whether a fact was declared as having actually happened, promoted out of the payload. * * ── What this was for (measured 2026-09-09) ─────────────────────────────── * The twin's journal held 1,134 rows and every one of them was produced by a generator. There * was no way to ask: * * select count(*) from twin_events 1134 * select count(*) where payload like '%SIM-%' 1134 * select count(*) where order_id like 'WO-SIM-%' 558 ← half of them * * The only signal was a naming convention on the sending side (`SIM-`, `WO-SIM-`), and the * column that convention reaches was empty on half the rows. Meanwhile the screens compute * yield and utilisation from those rows and cannot say what they are made of. * * ── The property that matters is the absent case ────────────────────────── * `null` means the sender did not declare, which is **not** "it actually happened". Reading * those two as the same thing is the whole defect: a generator that omits the field has its * output counted as production, and nothing says so. * * So most of what is asserted here is about absence, not about the two declared values. */ import { test } from 'node:test' import assert from 'node:assert/strict' import { provenanceOf, twinEventKeys } from '../server/service/twin-event/twin-event-keys.js' const envelope = (over: Record = {}) => ({ eventId: 'e1', eventType: 'task.status', eventTime: '2026-09-09T02:00:00.000Z', tenantId: 't1', data: { taskId: 'SIM-ECU-0001-01', status: 'completed' }, ...over }) test('★ a fact declared simulated says so in a column, not only inside the payload', () => { assert.equal(provenanceOf(envelope({ provenance: 'simulated' })), 'simulated') assert.equal(twinEventKeys(envelope({ provenance: 'simulated' })).provenance, 'simulated') }) test('a fact declared actual says that', () => { assert.equal(provenanceOf(envelope({ provenance: 'actual' })), 'actual') }) test('★ no declaration is not a declaration of actual — this is the defect being fixed', () => { /* * The payload here carries `SIM-…` in its task id, which is exactly how the 1,134 rows were * recognisable by hand. The column still says nothing, because the sender said nothing. That * is correct: the twin does not judge, it records what it was told. * * A reader has to distinguish three states. If it folds the last two together, a generator * that forgets the field is counted as production. */ assert.equal(provenanceOf(envelope()), undefined) assert.equal(twinEventKeys(envelope()).provenance, undefined) assert.notEqual(provenanceOf(envelope()), 'actual', 'absent must never resolve to actual') }) test('a value the source made up does not reach the index', () => { /* * Same discipline as `actionOf`, which takes only ADD · OBSERVE · DELETE. A column with three * possible values stops being answerable the moment a fourth arrives from a source we do not * control. */ for (const bogus of ['ACTUAL', 'Simulated', 'fake', 'true', '', 'sim', 1, true, null, {}]) { assert.equal(provenanceOf(envelope({ provenance: bogus })), undefined, `${JSON.stringify(bogus)} reached the column`) } }) test('the declaration is read from the envelope, not from the source data', () => { /* * `data` is where the source system writes. If this were read from there, a source could * declare its own facts actual by writing a key, and the declaration would no longer be the * connecting side's statement. The envelope is ours. */ const sneaky = envelope({ data: { taskId: 'x', provenance: 'actual' } }) assert.equal(provenanceOf(sneaky), undefined, 'a value inside data was promoted') }) test('promoting it does not disturb the keys already promoted', () => { /* * This function is the write path's only promotion point, so adding one key is checked * together with not disturbing the others. */ const full = twinEventKeys( envelope({ provenance: 'simulated', data: { action: 'OBSERVE', bizStep: 'commissioning', epcList: ['urn:epc:id:sgtin:1.2.3'], orderId: 'WO-1' } }) ) assert.equal(full.action, 'OBSERVE') assert.equal(full.bizStep, 'commissioning') assert.equal(full.epc, 'urn:epc:id:sgtin:1.2.3') assert.equal(full.orderId, 'WO-1') assert.equal(full.provenance, 'simulated') })