@substrat-run/contract-tests 0.137.0 → 0.138.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/directory-restore-suite.d.ts.map +1 -1
  2. package/dist/directory-restore-suite.js +40 -0
  3. package/dist/directory-restore-suite.js.map +1 -1
  4. package/dist/impersonation-suite.d.ts.map +1 -1
  5. package/dist/impersonation-suite.js +56 -0
  6. package/dist/impersonation-suite.js.map +1 -1
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +3 -0
  10. package/dist/index.js.map +1 -1
  11. package/dist/job-run-suite.d.ts.map +1 -1
  12. package/dist/job-run-suite.js +263 -1
  13. package/dist/job-run-suite.js.map +1 -1
  14. package/dist/membership-executor-suite.d.ts +15 -0
  15. package/dist/membership-executor-suite.d.ts.map +1 -0
  16. package/dist/membership-executor-suite.js +620 -0
  17. package/dist/membership-executor-suite.js.map +1 -0
  18. package/dist/membership-module.d.ts +17 -0
  19. package/dist/membership-module.d.ts.map +1 -0
  20. package/dist/membership-module.js +128 -0
  21. package/dist/membership-module.js.map +1 -0
  22. package/dist/modules.d.ts.map +1 -1
  23. package/dist/modules.js +19 -0
  24. package/dist/modules.js.map +1 -1
  25. package/dist/peer-suite.d.ts +10 -1
  26. package/dist/peer-suite.d.ts.map +1 -1
  27. package/dist/peer-suite.js +98 -0
  28. package/dist/peer-suite.js.map +1 -1
  29. package/dist/permission-suite.d.ts.map +1 -1
  30. package/dist/permission-suite.js +7 -7
  31. package/dist/permission-suite.js.map +1 -1
  32. package/dist/scope-host-suite.d.ts.map +1 -1
  33. package/dist/scope-host-suite.js +142 -34
  34. package/dist/scope-host-suite.js.map +1 -1
  35. package/dist/vertical-events-suite.d.ts.map +1 -1
  36. package/dist/vertical-events-suite.js +74 -1
  37. package/dist/vertical-events-suite.js.map +1 -1
  38. package/package.json +4 -4
@@ -1,6 +1,6 @@
1
1
  import { describe, it, expect, beforeAll, afterAll } from 'vitest';
2
2
  import { connectorCalls, connectorTestFetch, resetConnectorCalls } from './connector-fixture.js';
3
- import { connectionId, dataSubjectId, domainEvent, errorCodeOf, eventId, instant, moduleManifest, orgId, AUTO_ADMISSION_NOTE, permissionKey, platformActorId, platformRequestId, principalId, scopeId, tenantId, SCOPE_QUERY_ROW_MAX, KERNEL_AUTHORED_EVENT_TYPES, SWEEP_RUNS_KIND, SCOPE_TABLE_PAGE_MAX, } from '@substrat-run/contracts';
3
+ import { connectionId, dataSubjectId, domainEvent, errorCodeOf, moduleId, SCOPE_GATE_REASONS, eventId, instant, moduleManifest, orgId, AUTO_ADMISSION_NOTE, permissionKey, platformActorId, platformRequestId, principalId, scopeId, tenantId, SCOPE_QUERY_ROW_MAX, KERNEL_AUTHORED_EVENT_TYPES, SWEEP_RUNS_KIND, SCOPE_TABLE_PAGE_MAX, } from '@substrat-run/contracts';
4
4
  import { INERT_SCOPE_REASON, isSearchIndexTable, platformIntentFailureMessage, REDACTED_DELIVERY_NOTE, REDACTED_FAILURE_NOTE, REDACTED_INTENT_MARKER, runPlatformSweep, startPlatformSweeper, createUlid, ulid, } from '@substrat-run/kernel';
5
5
  import { billedMod, contractTestBareOps, contractTestInitialModules, gateModManifest, lateMod, TEST_LIFECYCLE, testModManifest, victimModManifest, } from './modules.js';
6
6
  /**
@@ -133,10 +133,11 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
133
133
  await expect(host.provisionScope(staff, { tenantId: t1, scopeId: s1, jurisdiction: 'eu' })).resolves.toBeUndefined();
134
134
  });
135
135
  it('refuses to provision a scope under a tenant with no record (§4.1)', async () => {
136
- await expect(host.provisionScope(staff, {
136
+ // `conflict`, not `not_found`: POST /scopes exists, and what is missing is its precondition.
137
+ await expectRefusal(host.provisionScope(staff, {
137
138
  tenantId: tenantId.parse(ulid()),
138
139
  scopeId: scopeId.parse(ulid()),
139
- })).rejects.toThrow(/unknown tenant/);
140
+ }), 'conflict');
140
141
  });
141
142
  it('fails closed on a mismatched (tenantId, scopeId) pair (K-3)', async () => {
142
143
  await expect(host.getScope(alice, t2, s1)).rejects.toThrow();
@@ -1903,6 +1904,52 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
1903
1904
  expect((await host.admin.accessLog(staff, { tenantId: t1, method: 'lifecycleFlow' })).length).toBeGreaterThan(0);
1904
1905
  });
1905
1906
  });
1907
+ describe('operationSeries on both adapters (#1750)', () => {
1908
+ // `readOperationSeries`'s own suite pins the counting over a hand-built outbox; this
1909
+ // proves the wiring — real emits, with real ids, read back through the platform verb.
1910
+ // On the Durable Object it is also the proof that `strftime`, `json_each` and the
1911
+ // primary-key seek run in workerd's SQLite.
1912
+ const MOVES = [{ entityType: 'test-lifecycle', operation: 'test/move' }];
1913
+ /** A window from the current whole second, a minute wide, in one-minute buckets. */
1914
+ const window = () => {
1915
+ const since = Math.floor(Date.now() / 1000) * 1000;
1916
+ return { moves: MOVES, since: new Date(since).toISOString(), until: new Date(since + 60_000).toISOString(), bucketMinutes: 1 };
1917
+ };
1918
+ const total = (r) => r.series[0].total;
1919
+ it('counts every call that made the move, inside the window', async () => {
1920
+ const stub = await host.getScope(alice, t1, s1);
1921
+ const w = window();
1922
+ const earlier = { ...w, since: new Date(Date.parse(w.since) - 120_000).toISOString(), until: w.since };
1923
+ const before = total(await host.admin.operationSeries(staff, t1, s1, w));
1924
+ const beforeEarlier = total(await host.admin.operationSeries(staff, t1, s1, earlier));
1925
+ await stub.invoke('test/move', { entityId: 'v1', state: 'draft' });
1926
+ await stub.invoke('test/move', { entityId: 'v1', state: 'live' });
1927
+ await stub.invoke('test/move', { entityId: 'v2', state: 'draft' });
1928
+ const after = await host.admin.operationSeries(staff, t1, s1, w);
1929
+ expect(total(after)).toBe(before + 3);
1930
+ expect(after.series[0].buckets.every((b) => Date.parse(b.start) >= Date.parse(w.since))).toBe(true);
1931
+ // The twin: a window that closed before the calls ran counts none of them. Compared
1932
+ // with itself, not with zero: earlier cases in this scope may have moved inside it.
1933
+ expect(total(await host.admin.operationSeries(staff, t1, s1, earlier))).toBe(beforeEarlier);
1934
+ });
1935
+ it("answers only for the tenant's own scope: a mismatched pair is refused, another tenant's scope counts none of it", async () => {
1936
+ const stub = await host.getScope(alice, t1, s1);
1937
+ const w = window();
1938
+ await stub.invoke('test/move', { entityId: 'v3', state: 'draft' });
1939
+ // The positive twin: t1's own pair sees the move.
1940
+ expect(total(await host.admin.operationSeries(staff, t1, s1, w))).toBeGreaterThanOrEqual(1);
1941
+ // K-3: never another tenant's outbox, and never an empty answer for one either.
1942
+ await expect(host.admin.operationSeries(staff, t2, s1, w)).rejects.toThrow();
1943
+ await expect(host.admin.operationSeries(staff, t1, s2, w)).rejects.toThrow();
1944
+ // t2's own scope answers from its own outbox, in which t1 moved nothing.
1945
+ expect(total(await host.admin.operationSeries(staff, t2, s2, w))).toBe(0);
1946
+ // K-24: the read is recorded against the actor, with the window and no operation names.
1947
+ const rows = await host.admin.accessLog(staff, { tenantId: t1, method: 'operationSeries' });
1948
+ expect(rows.length).toBeGreaterThan(0);
1949
+ expect(rows[0].params).toContain('test-lifecycle');
1950
+ expect(rows[0].params).not.toContain('test/move');
1951
+ });
1952
+ });
1906
1953
  it('records a refused transition after the rollback, and the replay counts it (#1745)', async () => {
1907
1954
  // The operation throws the platform's conflict, so its transaction rolls back — the
1908
1955
  // refusal is the one write that must survive it, on both adapters.
@@ -2005,7 +2052,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
2005
2052
  expect(page.limit).toBe(1);
2006
2053
  });
2007
2054
  it('rejects an unknown table', async () => {
2008
- await expect(host.admin.readScopeTable(staff, t1, s1, { table: 'no_such_table', limit: 50, offset: 0 })).rejects.toThrow(/unknown table/);
2055
+ await expectRefusal(host.admin.readScopeTable(staff, t1, s1, { table: 'no_such_table', limit: 50, offset: 0 }), 'not_found');
2009
2056
  });
2010
2057
  it('refuses a page bound SQLite would misread, and still clamps to the ceiling (#1632)', async () => {
2011
2058
  // The clamp passed NaN straight through to LIMIT / OFFSET. Refused instead, with the
@@ -2104,9 +2151,9 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
2104
2151
  `WITH x AS (SELECT v FROM marker) INSERT INTO marker (v) SELECT v FROM x`,
2105
2152
  ];
2106
2153
  for (const sql of writes) {
2107
- // The message prefix is CONTRACT: the transport maps /read-only console/
2108
- // to a 400, so both adapters must refuse with it (errors.ts).
2109
- await expect(host.admin.queryScope(staff, t1, s1, { sql })).rejects.toThrow(/read-only console/);
2154
+ // The code is what the transport renders (a 400). The prefix stays contract too:
2155
+ // a CP-less vertical's console route and the dashboard still recognise it by it.
2156
+ await expectRefusal(host.admin.queryScope(staff, t1, s1, { sql }), 'validation_failed', /read-only console/);
2110
2157
  }
2111
2158
  // No write happened and nothing was forged: the marker rows are intact.
2112
2159
  const after = await host.admin.queryScope(staff, t1, s1, {
@@ -3759,7 +3806,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
3759
3806
  await expect(host.admin.exportScope(staff, t1, snapId)).rejects.toThrow();
3760
3807
  });
3761
3808
  it('refuses to delete a primary scope (forkedFrom is null)', async () => {
3762
- await expect(host.deleteSnapshot(staff, t1, s1)).rejects.toThrow(/not a fork/);
3809
+ await expectRefusal(host.deleteSnapshot(staff, t1, s1), 'forbidden', /not a fork/);
3763
3810
  // Still there, still readable.
3764
3811
  expect((await host.admin.getScopeRecord(staff, t1, s1))?.id).toBe(s1);
3765
3812
  });
@@ -5448,7 +5495,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
5448
5495
  });
5449
5496
  it('refuses a promotion that changes permissions until it is acknowledged', async () => {
5450
5497
  const v2 = await publish('2.1.0', { perm: 'pB', mig: 'gA' }); // permissions moved
5451
- await expect(host.admin.promoteVersion(staff, 'callout', 'prod', v2)).rejects.toThrow(/changes the permission surface/);
5498
+ await expectRefusal(host.admin.promoteVersion(staff, 'callout', 'prod', v2), 'conflict', /changes the permission surface/);
5452
5499
  // Still pointing at the old one — a refused promotion changes nothing.
5453
5500
  const before = await host.admin.listChannels(staff, 'callout');
5454
5501
  expect(before.find((c) => c.channel === 'prod')?.versionId).not.toBe(v2);
@@ -5458,7 +5505,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
5458
5505
  });
5459
5506
  it('refuses a promotion that changes migrations until it is acknowledged', async () => {
5460
5507
  const v3 = await publish('2.2.0', { perm: 'pB', mig: 'gB' }); // migrations moved
5461
- await expect(host.admin.promoteVersion(staff, 'callout', 'prod', v3)).rejects.toThrow(/changes migrations/);
5508
+ await expectRefusal(host.admin.promoteVersion(staff, 'callout', 'prod', v3), 'conflict', /changes migrations/);
5462
5509
  await host.admin.promoteVersion(staff, 'callout', 'prod', v3, { migrationChange: true });
5463
5510
  expect((await host.admin.listChannels(staff, 'callout')).find((c) => c.channel === 'prod')
5464
5511
  ?.versionId).toBe(v3);
@@ -5673,7 +5720,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
5673
5720
  const sc = scopeId.parse(ulid());
5674
5721
  await host.admin.createTenant(staff, { id: t, slug: `t-${t.toLowerCase()}`, name: 'T' });
5675
5722
  await host.provisionScope(staff, { tenantId: t, scopeId: sc, jurisdiction: 'eu' });
5676
- await expect(host.getScope(alice, t, sc)).rejects.toThrow(/not active/);
5723
+ await expectRefusal(host.getScope(alice, t, sc), 'conflict', /not active/);
5677
5724
  await host.admin.activateScope(staff, t, sc);
5678
5725
  await expect(host.getScope(alice, t, sc)).resolves.toBeDefined();
5679
5726
  });
@@ -5790,7 +5837,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
5790
5837
  canonical: true,
5791
5838
  });
5792
5839
  await bind(holder);
5793
- await expect(bind(claimant)).rejects.toThrow(/already bound to another scope/);
5840
+ await expectRefusal(bind(claimant), 'conflict', /already bound to another scope/);
5794
5841
  await host.admin.archiveScope(staff, t1, holder);
5795
5842
  await bind(claimant); // the holder released the name
5796
5843
  await host.admin.setHostnameStatus(staff, 'reclaim.example.com', 'active');
@@ -5905,14 +5952,14 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
5905
5952
  it('refuses to move a hostname to another scope', async () => {
5906
5953
  // A hostname routes to exactly one place. Silently rebinding would move
5907
5954
  // another tenant's traffic.
5908
- await expect(host.admin.bindHostname(staff, {
5955
+ await expectRefusal(host.admin.bindHostname(staff, {
5909
5956
  hostname: 'acme.example.com',
5910
5957
  tenantId: t2,
5911
5958
  scopeId: s2,
5912
5959
  surface: 'app',
5913
5960
  region: null,
5914
5961
  canonical: false,
5915
- })).rejects.toThrow(/already bound to another scope/);
5962
+ }), 'conflict', /already bound to another scope/);
5916
5963
  });
5917
5964
  it('carries the region, which is what Regional Services is set from', async () => {
5918
5965
  // Residency is per hostname, which is why it lives here rather than in a
@@ -6822,7 +6869,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
6822
6869
  await host.admin.activateScope(staff, t3, s3);
6823
6870
  await expect(host.getScope(alice, t3, s3)).resolves.toBeDefined();
6824
6871
  await host.admin.setTenantStatus(staff, t3, 'suspended');
6825
- await expect(host.getScope(alice, t3, s3)).rejects.toThrow(/not active/);
6872
+ await expectRefusal(host.getScope(alice, t3, s3), 'conflict', /tenant not active/);
6826
6873
  await host.admin.setTenantStatus(staff, t3, 'active');
6827
6874
  await expect(host.getScope(alice, t3, s3)).resolves.toBeDefined();
6828
6875
  });
@@ -6833,7 +6880,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
6833
6880
  expect(suspend.before.status).toBe('active');
6834
6881
  });
6835
6882
  it('rejects a status transition on an unknown tenant', async () => {
6836
- await expect(host.admin.setTenantStatus(staff, tenantId.parse(ulid()), 'suspended')).rejects.toThrow(/unknown tenant/);
6883
+ await expectRefusal(host.admin.setTenantStatus(staff, tenantId.parse(ulid()), 'suspended'), 'not_found');
6837
6884
  });
6838
6885
  it('renames a tenant display name — audited with before/after, slug untouched', async () => {
6839
6886
  const before = (await host.admin.getTenant(staff, t3));
@@ -6848,13 +6895,13 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
6848
6895
  // Renaming to the current name is a no-op and leaves no audit row.
6849
6896
  await host.admin.setTenantName(staff, t3, 'Renamed Co');
6850
6897
  expect((await host.admin.auditLog(staff, { tenantId: t3 })).filter((r) => r.action === 'setTenantName')).toHaveLength(1);
6851
- await expect(host.admin.setTenantName(staff, tenantId.parse(ulid()), 'Ghost')).rejects.toThrow(/unknown tenant/);
6898
+ await expectRefusal(host.admin.setTenantName(staff, tenantId.parse(ulid()), 'Ghost'), 'not_found');
6852
6899
  });
6853
6900
  // -- scope lifecycle (control-plane.md §4.2) ------------------------------
6854
6901
  it('suspend/unsuspend a scope gates getScope for that scope alone (§4.2)', async () => {
6855
6902
  // s3 is active (reactivated above). Suspending it fails closed…
6856
6903
  await host.admin.suspendScope(staff, t3, s3);
6857
- await expect(host.getScope(alice, t3, s3)).rejects.toThrow(/scope not active/);
6904
+ await expectRefusal(host.getScope(alice, t3, s3), 'conflict', /scope not active/);
6858
6905
  // …while a sibling scope under the same tenant is untouched.
6859
6906
  const sibling = scopeId.parse(ulid());
6860
6907
  await host.provisionScope(staff, { tenantId: t3, scopeId: sibling, jurisdiction: 'eu' });
@@ -6866,7 +6913,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
6866
6913
  });
6867
6914
  it('archive then un-archive is an explicit audited restore (§4.2)', async () => {
6868
6915
  await host.admin.archiveScope(staff, t3, s3);
6869
- await expect(host.getScope(alice, t3, s3)).rejects.toThrow(/scope not active/);
6916
+ await expectRefusal(host.getScope(alice, t3, s3), 'conflict', /scope not active/);
6870
6917
  await host.admin.unarchiveScope(staff, t3, s3);
6871
6918
  await expect(host.getScope(alice, t3, s3)).resolves.toBeDefined();
6872
6919
  const transitions = (await host.admin.auditLog(staff, { tenantId: t3 })).filter((r) => r.action === 'archiveScope' || r.action === 'unarchiveScope');
@@ -6878,7 +6925,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
6878
6925
  });
6879
6926
  it('rejects an illegal scope transition, fail closed (§4.2)', async () => {
6880
6927
  // s3 is active — you cannot un-archive an active scope.
6881
- await expect(host.admin.unarchiveScope(staff, t3, s3)).rejects.toThrow(/illegal scope transition/);
6928
+ await expectRefusal(host.admin.unarchiveScope(staff, t3, s3), 'conflict', /illegal scope transition/);
6882
6929
  });
6883
6930
  // -- reap: the terminal storage wipe (control-plane.md §4.4) ---------------
6884
6931
  it('archive stamps archivedAt; unarchive clears it (§4.4)', async () => {
@@ -6903,7 +6950,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
6903
6950
  await host.provisionScope(staff, { tenantId: t3, scopeId: s, slug: 'reap-me', jurisdiction: 'eu' });
6904
6951
  await host.admin.activateScope(staff, t3, s);
6905
6952
  // Reap refuses a live (active) scope — only archived may be reaped.
6906
- await expect(host.admin.reapScope(staff, t3, s)).rejects.toThrow(/not archived/);
6953
+ await expectRefusal(host.admin.reapScope(staff, t3, s), 'conflict', /not archived/);
6907
6954
  await host.admin.archiveScope(staff, t3, s);
6908
6955
  await host.admin.reapScope(staff, t3, s);
6909
6956
  // The directory row SURVIVES as a tombstone, now `reaped`.
@@ -6917,10 +6964,10 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
6917
6964
  // "this scope has no events" where the truth is "this scope is gone".
6918
6965
  await expect(host.admin.facetEvents(staff, t3, s, { groupBy: { kind: 'type' } })).rejects.toThrow(/reaped/);
6919
6966
  await expect(host.admin.entityHistory(staff, t3, s, { entityType: 'test-thing', entityId: 'x1' })).rejects.toThrow(/reaped/);
6920
- await expect(host.admin.listScopeTables(staff, t3, s)).rejects.toThrow(/reaped/);
6967
+ await expectRefusal(host.admin.listScopeTables(staff, t3, s), 'conflict', /reaped/);
6921
6968
  // #1524: a size read is refused BEFORE it reaches the storage. Addressing a reaped
6922
6969
  // DO to ask its size would recreate an empty database and report that as the scope.
6923
- await expect(host.admin.scopeDatabaseSize(staff, t3, s)).rejects.toThrow(/reaped/);
6970
+ await expectRefusal(host.admin.scopeDatabaseSize(staff, t3, s), 'conflict', /reaped/);
6924
6971
  // Audited as reapScope against the right scope + actor.
6925
6972
  const reapEntry = (await host.admin.auditLog(staff, { tenantId: t3 })).find((r) => r.action === 'reapScope' && r.scopeId === s);
6926
6973
  expect(reapEntry?.actor).toBe(staff);
@@ -6955,8 +7002,8 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
6955
7002
  await host.admin.activateScope(staff, t3, fresh);
6956
7003
  await expect(host.getScope(alice, t3, fresh)).resolves.toBeDefined();
6957
7004
  // Terminal: a reaped scope cannot be unarchived (bytes are gone) or reaped again.
6958
- await expect(host.admin.unarchiveScope(staff, t3, s)).rejects.toThrow(/illegal scope transition/);
6959
- await expect(host.admin.reapScope(staff, t3, s)).rejects.toThrow(/not archived/);
7005
+ await expectRefusal(host.admin.unarchiveScope(staff, t3, s), 'conflict', /illegal scope transition/);
7006
+ await expectRefusal(host.admin.reapScope(staff, t3, s), 'conflict', /not archived/);
6960
7007
  });
6961
7008
  it('reap refuses while a hostname still resolves; unbind first, then it reaps (§4.4)', async () => {
6962
7009
  // The regression this guards: a console `archiveScope` (unlike the dashboard delete
@@ -6977,7 +7024,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
6977
7024
  // Archive alone (the console path) leaves the hostname bound…
6978
7025
  await host.admin.archiveScope(staff, t3, s);
6979
7026
  // …so reap refuses, naming the offending hostname, and the row survives untouched.
6980
- await expect(host.admin.reapScope(staff, t3, s)).rejects.toThrow(/still resolves hostname 'still-serving\.example\.com'/);
7027
+ await expectRefusal(host.admin.reapScope(staff, t3, s), 'conflict', /still resolves hostname 'still-serving\.example\.com'/);
6981
7028
  expect((await host.admin.getScopeRecord(staff, t3, s)).status).toBe('archived');
6982
7029
  // Unbinding is the visible, reversible step that clears the wall…
6983
7030
  await host.admin.unbindHostname(staff, 'still-serving.example.com');
@@ -6998,7 +7045,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
6998
7045
  const deleting = (await host.admin.getTenant(staff, t));
6999
7046
  expect(deleting.status).toBe('deleting');
7000
7047
  expect(deleting.deletingAt).not.toBeNull();
7001
- await expect(host.getScope(alice, t, s)).rejects.toThrow(/not active/);
7048
+ await expectRefusal(host.getScope(alice, t, s), 'conflict', /tenant not active/);
7002
7049
  // Un-delete (→ active) is a full restore and clears deletingAt.
7003
7050
  await host.admin.setTenantStatus(staff, t, 'active');
7004
7051
  const restored = (await host.admin.getTenant(staff, t));
@@ -7024,7 +7071,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
7024
7071
  expect((await host.admin.listOrgs(staff, t)).filter((o) => o.id === org)).toHaveLength(1);
7025
7072
  expect((await host.admin.resolveIdentity(t, `oidc:reap-${t.toLowerCase()}`, 'x1'))?.principal).toBe(person);
7026
7073
  // Reap only follows the reversible delete state.
7027
- await expect(host.admin.reapTenant(staff, t)).rejects.toThrow(/not deleting/);
7074
+ await expectRefusal(host.admin.reapTenant(staff, t), 'conflict', /not deleting/);
7028
7075
  // The caller reaps the scope first (the sweep/route orchestrates this above the
7029
7076
  // kernel); reapTenant itself is directory-side only.
7030
7077
  await host.admin.setTenantStatus(staff, t, 'deleting');
@@ -7049,11 +7096,72 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
7049
7096
  expect(reapEntry.before.status).toBe('deleting');
7050
7097
  expect(reapEntry.after.status).toBe('reaped');
7051
7098
  // Terminal: `reaped` is unreachable via setTenantStatus and reapTenant refuses it.
7052
- await expect(host.admin.setTenantStatus(staff, t, 'reaped')).rejects.toThrow(/cannot be set to 'reaped'/);
7053
- await expect(host.admin.reapTenant(staff, t)).rejects.toThrow(/not deleting/);
7099
+ await expectRefusal(host.admin.setTenantStatus(staff, t, 'reaped'), 'validation_failed', /cannot be set to 'reaped'/);
7100
+ await expectRefusal(host.admin.reapTenant(staff, t), 'conflict', /not deleting/);
7054
7101
  });
7055
7102
  it('rejects reapTenant on an unknown tenant', async () => {
7056
- await expect(host.admin.reapTenant(staff, tenantId.parse(ulid()))).rejects.toThrow(/unknown tenant/);
7103
+ await expectRefusal(host.admin.reapTenant(staff, tenantId.parse(ulid())), 'not_found');
7104
+ });
7105
+ // #113: the last directory refusals the control plane matched by message, each typed now.
7106
+ // Asserted on the code — the thing a transport renders — with its positive twin beside it.
7107
+ it('marks the scope gate\'s refusal as the gate\'s, so a public edge can answer it neutrally (#113)', async () => {
7108
+ const t = tenantId.parse(ulid());
7109
+ const sc = scopeId.parse(ulid());
7110
+ await host.admin.createTenant(staff, { id: t, slug: `gate-${t.toLowerCase()}`, name: 'Gate Co' });
7111
+ await host.provisionScope(staff, { tenantId: t, scopeId: sc });
7112
+ await host.admin.activateScope(staff, t, sc);
7113
+ await host.admin.suspendScope(staff, t, sc);
7114
+ const scopeHeld = await host.getScope(alice, t, sc).then(() => undefined, (e) => e);
7115
+ expect(errorCodeOf(scopeHeld)).toBe('conflict');
7116
+ expect(scopeHeld.extensions.reason).toBe(SCOPE_GATE_REASONS.notActive);
7117
+ await host.admin.unsuspendScope(staff, t, sc);
7118
+ await host.admin.setTenantStatus(staff, t, 'suspended');
7119
+ const tenantHeld = await host.getScope(alice, t, sc).then(() => undefined, (e) => e);
7120
+ expect(tenantHeld.extensions.reason).toBe(SCOPE_GATE_REASONS.notActive);
7121
+ // The twin: an admin refusal of the same code is not the gate's.
7122
+ const illegal = await host.admin.unarchiveScope(staff, t, sc).then(() => undefined, (e) => e);
7123
+ expect(errorCodeOf(illegal)).toBe('conflict');
7124
+ expect(illegal.extensions.reason).toBeUndefined();
7125
+ });
7126
+ it('refuses what used to answer the generic 500 with its own code (#113)', async () => {
7127
+ const t = tenantId.parse(ulid());
7128
+ const sc = scopeId.parse(ulid());
7129
+ await host.admin.createTenant(staff, { id: t, slug: `was500-${t.toLowerCase()}`, name: 'Was 500' });
7130
+ await host.provisionScope(staff, { tenantId: t, scopeId: sc });
7131
+ await host.admin.activateScope(staff, t, sc);
7132
+ // An unknown hostname, on both of its doors.
7133
+ await expectRefusal(host.admin.setHostnameStatus(staff, `ghost-${t.toLowerCase()}.example.com`, 'active'), 'not_found', /unknown hostname/);
7134
+ // A link into a pool nobody registered: its precondition is missing.
7135
+ await expectRefusal(host.admin.linkIdentity(staff, { provider: `oidc:ghost-${t.toLowerCase()}`, externalId: 'u1', principal: alice, tenantId: t }), 'conflict', /is not registered/);
7136
+ // A system door for a module this host never registered.
7137
+ await expectRefusal(host.getSystemScope(moduleId.parse('@ghost/none'), t, sc), 'not_found', /module not registered/);
7138
+ // A migration asked of a scope that is archived.
7139
+ await host.admin.archiveScope(staff, t, sc);
7140
+ await expectRefusal(host.migrateScope(t, sc), 'conflict', /not migratable \(status: archived\)/);
7141
+ });
7142
+ it('refuses to provision a scope under a non-active tenant, typed conflict (#113)', async () => {
7143
+ const t = tenantId.parse(ulid());
7144
+ await host.admin.createTenant(staff, { id: t, slug: `held-${t.toLowerCase()}`, name: 'Held Co' });
7145
+ await host.admin.setTenantStatus(staff, t, 'suspended');
7146
+ await expectRefusal(host.provisionScope(staff, { tenantId: t, scopeId: scopeId.parse(ulid()) }), 'conflict', /non-active tenant \(status: suspended\)/);
7147
+ await host.admin.setTenantStatus(staff, t, 'active');
7148
+ await host.provisionScope(staff, { tenantId: t, scopeId: scopeId.parse(ulid()) }); // the twin: active again, it provisions
7149
+ });
7150
+ it('refuses an identity pool re-registered with another topology, typed conflict (#113)', async () => {
7151
+ const t = tenantId.parse(ulid());
7152
+ const provider = `oidc:pool-${t.toLowerCase()}`;
7153
+ await host.admin.createTenant(staff, { id: t, slug: `pool-${t.toLowerCase()}`, name: 'Pool Co' });
7154
+ await host.admin.registerIdentityPool(staff, { provider, topology: 'tenant-bound', tenantId: t });
7155
+ // The same declaration again is idempotent…
7156
+ await expect(host.admin.registerIdentityPool(staff, { provider, topology: 'tenant-bound', tenantId: t })).resolves.toBeUndefined();
7157
+ // …a different one conflicts.
7158
+ await expectRefusal(host.admin.registerIdentityPool(staff, { provider, topology: 'central', tenantId: null }), 'conflict', /is already registered as tenant-bound/);
7159
+ });
7160
+ it('refuses a vertical re-registered from another source, typed conflict (#113)', async () => {
7161
+ const slug = `src-${ulid().toLowerCase()}`;
7162
+ await host.admin.registerVertical(staff, { slug, name: 'Source Test', source: 'git' });
7163
+ await expect(host.admin.registerVertical(staff, { slug, name: 'Source Test', source: 'git' })).resolves.toBeUndefined();
7164
+ await expectRefusal(host.admin.registerVertical(staff, { slug, name: 'Source Test', source: 'cli' }), 'conflict', /is already registered as git/);
7057
7165
  });
7058
7166
  // -- identity links: the projection read (#406) ---------------------------
7059
7167
  it('listIdentityLinks returns one tenant’s links and tracks link/unlink', async () => {
@@ -7347,7 +7455,7 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
7347
7455
  });
7348
7456
  it('refuses a slug already taken under the tenant, and re-provision is still idempotent (§3.2)', async () => {
7349
7457
  const other = scopeId.parse(ulid());
7350
- await expect(host.provisionScope(staff, { tenantId: t5, scopeId: other, slug: 'brf-vasastan' })).rejects.toThrow(/already taken/);
7458
+ await expectRefusal(host.provisionScope(staff, { tenantId: t5, scopeId: other, slug: 'brf-vasastan' }), 'conflict', /already taken/);
7351
7459
  // Idempotency is keyed on the scope id, so re-provisioning the SAME scope
7352
7460
  // must not collide with its own slug.
7353
7461
  const named = (await host.admin.listScopes(staff, { tenantId: t5 })).find((s) => s.slug === 'brf-vasastan');
@@ -7362,11 +7470,11 @@ export function scopeHostContractSuite(adapterName, makeFixture, opts = {}) {
7362
7470
  it('refuses a tenant slug already taken, fail closed (§4.1)', async () => {
7363
7471
  // INSERT OR IGNORE would have reported this as an idempotent no-op and
7364
7472
  // silently not created the tenant the caller asked for.
7365
- await expect(host.admin.createTenant(staff, {
7473
+ await expectRefusal(host.admin.createTenant(staff, {
7366
7474
  id: tenantId.parse(ulid()),
7367
7475
  slug: 'directory-co',
7368
7476
  name: 'Impostor Co',
7369
- })).rejects.toThrow(/already taken/);
7477
+ }), 'conflict', /already taken/);
7370
7478
  });
7371
7479
  it('enumerates the scopes under a tenant, and filters by status (§4.5)', async () => {
7372
7480
  const all = await host.admin.listScopes(staff, { tenantId: t5 });