/* * 런타임 키 — **트윈의 정체성은 `(도메인, 인스턴스 id)`.** * * 계기(2026-08-06): 런타임 레지스트리가 `instanceId` 하나로 키를 잡고 있었다. 레퍼런스 경로가 소스 * 이름을 그대로 id 로 쓰므로(`sap-ewm-1710`) 두 테넌트가 같은 벤더 시스템에 연결하면 같은 id 가 된다 — * 그때 남의 커널에 이벤트가 적용되거나, 웜스타트 씨앗이 남의 스냅샷이 됐다. * * 이 테스트가 지키는 것은 **키가 모호해지지 않는 것** 하나다. 모호한 키는 오류 없이 틀린 값을 낸다. */ import { test } from 'node:test' import assert from 'node:assert/strict' import { RUNTIME_KEY_SEP, isOfDomain, parseRuntimeKey, runtimeKey } from '../server/engine/runtime-key.ts' const D1 = '11111111-1111-1111-1111-111111111111' const D2 = '22222222-2222-2222-2222-222222222222' test('같은 인스턴스 id 라도 테넌트가 다르면 다른 키다 — 이 작업의 요점', () => { assert.notEqual(runtimeKey(D1, 'sap-ewm-1710'), runtimeKey(D2, 'sap-ewm-1710')) }) test('같은 테넌트·같은 id 는 같은 키다 — 재기동이 자기 자리를 찾는다', () => { assert.equal(runtimeKey(D1, 'hatiolab-wms'), runtimeKey(D1, 'hatiolab-wms')) }) test('되돌리면 원래 둘이 나온다', () => { const { domainId, instanceId } = parseRuntimeKey(runtimeKey(D1, 'hatiolab-wms')) assert.equal(domainId, D1) assert.equal(instanceId, 'hatiolab-wms') }) test('인스턴스 id 안의 구분자를 보존한다 — 첫 구분자에서만 자른다', () => { /* 레퍼런스 소스 이름에 `:` 가 들어갈 수 있다. 마지막 구분자에서 자르면 id 가 잘려 다른 트윈이 된다. */ const weird = 'urn:epc:id:sgln:0614141.12345' const { domainId, instanceId } = parseRuntimeKey(runtimeKey(D1, weird)) assert.equal(domainId, D1) assert.equal(instanceId, weird) }) test('도메인 id 에 구분자가 있으면 오류를 낸다 — 알리지 않고 모호한 키를 만들지 않는다', () => { /* 알리지 않고 만들면 그게 곧 이 결함의 재발이다(어느 테넌트인지 알 수 없는 키). */ assert.throws(() => runtimeKey(`a${RUNTIME_KEY_SEP}b`, 'x'), /ambiguous/) }) test('빈 값은 키가 될 수 없다 — 테넌트 없는 런타임은 없다', () => { assert.throws(() => runtimeKey('', 'x'), /domainId is required/) assert.throws(() => runtimeKey(D1, ''), /instanceId is required/) }) test('키가 아닌 것을 되돌리려 하면 오류를 낸다', () => { for (const bad of ['', 'no-separator', `${RUNTIME_KEY_SEP}leading`, `trailing${RUNTIME_KEY_SEP}`]) { assert.throws(() => parseRuntimeKey(bad), /is not a runtime key/, `입력: "${bad}"`) } }) test('도메인으로 걸러낸다 — 접두사가 아니라 경계까지 본다', () => { const key = runtimeKey(D1, 'wms') assert.equal(isOfDomain(key, D1), true) assert.equal(isOfDomain(key, D2), false) /* 도메인 id 의 앞부분만 같은 것에 걸리지 않는다(구분자까지 붙여 본다). */ assert.equal(isOfDomain(key, D1.slice(0, 8)), false) assert.equal(isOfDomain(key, ''), false) })