/* * ★ A twin made by one producer is not rebuilt by another (chief architect ruling, 2026-09-24). * * On 09-24 the warehouse connection `wms-miratek`, imported with site id `miratek`, landed on the running * plant mirror `miratek` and replaced its structure. The row already named its producer. These pin the * rule, and the last test pins where it runs: before `ingestMaster` writes anything. */ import { test } from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { INSTANCE_TAKEN, producerOf, refuseOverwriteByAnotherProducer } from '../server/engine/producer-guard.ts' const PLANT = { kind: 'reference', source: 'mes-miratek' } test('a different connection is refused, and the refusal names the connection that made the twin', () => { const r = refuseOverwriteByAnotherProducer('miratek', PLANT, { kind: 'reference', source: 'wms-miratek', siteId: 'miratek' }) assert.equal(r?.code, INSTANCE_TAKEN) assert.deepEqual(r?.params, { instance: 'miratek', producer: 'mes-miratek' }) }) test('the same producer re-reading its own twin passes, whatever else its origin carries', () => { /* Import sites carry { kind, source } at one call and add siteId · spaceId at another (resolver 168 vs 889). */ assert.equal(refuseOverwriteByAnotherProducer('miratek', PLANT, { ...PLANT, siteId: 'miratek', spaceId: 'miratek' }), null) /* Resync hands the stored origin back as it is (masterFromOrigin). */ assert.equal(refuseOverwriteByAnotherProducer('miratek', { ...PLANT, siteId: 'miratek' }, { ...PLANT, siteId: 'miratek' }), null) /* A template re-run with other knobs is the same template. */ assert.equal( refuseOverwriteByAnotherProducer('w1', { kind: 'template', templateId: 'wms-unit-load', params: { racks: 4 } }, { kind: 'template', templateId: 'wms-unit-load', params: { racks: 9 } }), null ) }) test('a template and a reference are different producers even with the same name', () => { assert.equal(refuseOverwriteByAnotherProducer('x', { kind: 'template', templateId: 'x' }, { kind: 'reference', source: 'x' })?.code, INSTANCE_TAKEN) }) test('(a) a twin nobody can name is not rebuilt by a declared producer', () => { const r = refuseOverwriteByAnotherProducer('old', undefined, PLANT) assert.deepEqual(r?.params, { instance: 'old', producer: 'undeclared' }) }) test('(b) a declared twin is not rebuilt by an undeclared producer', () => { assert.deepEqual(refuseOverwriteByAnotherProducer('miratek', PLANT, undefined)?.params, { instance: 'miratek', producer: 'mes-miratek' }) }) test('(c) neither declared passes — the demo seed re-ingesting its own twin at boot', () => { assert.equal(refuseOverwriteByAnotherProducer('rosarito-mes', undefined, undefined), null) assert.equal(refuseOverwriteByAnotherProducer('rosarito-mes', {}, { kind: '' }), null) }) test('a kind the guard does not know is still somebody, not nobody', () => { assert.notEqual(producerOf({ kind: 'bench', source: 'hatio-yard' }), undefined) }) test('ingestMaster refuses before its first write — a refusal after it would leave the twin half-changed', () => { const engine = readFileSync(fileURLToPath(new URL('../server/engine/twin-engine.ts', import.meta.url)), 'utf-8') const at = engine.indexOf('static async ingestMaster(') const body = engine.slice(at, engine.indexOf('\n static ', at + 10)) const guard = body.indexOf('refuseOverwriteByAnotherProducer(') const firstWrite = body.search(/\.save\(|\.update\(|\.insert\(|\.delete\(/) assert.ok(guard > 0, 'ingestMaster no longer asks who made the twin') assert.ok(firstWrite > 0, 'no write found — this check would pass by finding nothing') assert.ok(guard < firstWrite, 'the refusal must come before the first write') })