11import { expect , it , vi } from "vitest" ;
2- import type { LegacyCronRepairState } from "../commands/doctor/cron/legacy-repair.js" ;
2+ import type {
3+ CronOwnerProjection ,
4+ LegacyCronRepairState ,
5+ } from "../commands/doctor/cron/legacy-repair.js" ;
36import {
47 isCronOwnerWriteRefusalError ,
58 prepareCronOwnerWriteRefusal ,
69} from "./io.cron-owner-refusal.js" ;
710import { assertAutomaticBindingsWriteAllowed } from "./io.ownership-write-guard.js" ;
811
9- const state = ( rawJobs : Array < Record < string , unknown > > ) =>
10- ( { rawJobs, projectedOwnersByJobId : new Map ( ) } ) as unknown as LegacyCronRepairState ;
11- const deps = ( activeGateway ?: { pid : number ; port : number } , jobs ?: Record < string , unknown > [ ] ) => ( {
12+ const state = (
13+ rawJobs : Array < Record < string , unknown > > ,
14+ invalidConfigRows : Array < Record < string , unknown > > = [ ] ,
15+ projectedOwnersByJobId : ReadonlyMap < string , CronOwnerProjection > = new Map ( ) ,
16+ ) =>
17+ ( {
18+ rawJobs,
19+ invalidConfigRows,
20+ projectedOwnersByJobId,
21+ } ) as unknown as LegacyCronRepairState ;
22+ const deps = (
23+ activeGateway ?: {
24+ pid : number ;
25+ port : number ;
26+ cronOwnerProjection ?: "dynamic-default-v1" ;
27+ } ,
28+ jobs ?: Record < string , unknown > [ ] ,
29+ ) => ( {
1230 readActiveGatewayLockIdentity : vi . fn ( async ( ) =>
1331 activeGateway ? { ...activeGateway , createdAt : new Date ( 0 ) . toISOString ( ) } : undefined ,
1432 ) ,
1533 loadLegacyCronRepairState : vi . fn ( async ( ) => ( jobs ? state ( jobs ) : null ) ) ,
1634 materializeLegacyDefaultCronJobOwners : vi . fn ( ( ) => 0 ) ,
1735} ) ;
36+ const cfg = { agents : { entries : { ops : { } } } } ;
1837
19- it ( "refuses unsafe ownership writes and rechecks at commit" , async ( ) => {
38+ it ( "allows an ownership-safe live Gateway with a projected dynamic row and rechecks at commit" , async ( ) => {
39+ const injected = deps (
40+ {
41+ pid : process . pid + 1 ,
42+ port : 18_789 ,
43+ cronOwnerProjection : "dynamic-default-v1" ,
44+ } ,
45+ [ { id : "dynamic" } ] ,
46+ ) ;
47+ injected . loadLegacyCronRepairState . mockResolvedValueOnce (
48+ state (
49+ [ { id : "dynamic" } ] ,
50+ [ ] ,
51+ new Map ( [ [ "dynamic" , { kind : "runtime-default" as const , agentId : "ops" } ] ] ) ,
52+ ) ,
53+ ) ;
54+ const plan = await prepareCronOwnerWriteRefusal ( cfg , { storePath : "/tmp/cron.json" } , injected ) ;
55+ injected . loadLegacyCronRepairState . mockResolvedValueOnce ( state ( [ { id : "ownerless" } ] ) ) ;
56+ await expect ( plan . recheck ( ) ) . rejects . toThrow ( "ownerless legacy cron job" ) ;
57+ } ) ;
58+
59+ it ( "refuses an unproven live Gateway without suggesting doctor" , async ( ) => {
2060 const injected = deps ( { pid : process . pid + 1 , port : 18_789 } ) ;
21- await expect (
22- prepareCronOwnerWriteRefusal ( { storePath : "/tmp/cron.json" } , injected ) ,
23- ) . rejects . toThrow ( "live external Gateway " ) ;
24- expect ( injected . loadLegacyCronRepairState ) . not . toHaveBeenCalled ( ) ;
61+ const refusal = prepareCronOwnerWriteRefusal ( cfg , { storePath : "/tmp/cron.json" } , injected ) ;
62+ await expect ( refusal ) . rejects . toThrow ( "live external Gateway" ) ;
63+ await expect ( refusal ) . rejects . not . toThrow ( "doctor --fix " ) ;
64+ } ) ;
2565
66+ it . each ( [
67+ [ "without a live Gateway" , undefined ] ,
68+ [
69+ "with an ownership-safe live Gateway" ,
70+ {
71+ pid : process . pid + 1 ,
72+ port : 18_789 ,
73+ cronOwnerProjection : "dynamic-default-v1" as const ,
74+ } ,
75+ ] ,
76+ ] ) ( "refuses ownerless and corrupt cron rows %s" , async ( _name , activeGateway ) => {
2677 await expect (
2778 prepareCronOwnerWriteRefusal (
79+ cfg ,
2880 { storePath : "/tmp/cron.json" } ,
29- deps ( undefined , [
81+ deps ( activeGateway , [
3082 { id : "null" , agentId : null } ,
3183 { id : "blank" , agentId : " " } ,
3284 ] ) ,
3385 ) ,
3486 ) . rejects . toThrow ( "contains 2 ownerless legacy cron job" ) ;
3587
36- const commitDeps = deps ( undefined , [ { id : "owned" , agentId : "ops" } ] ) ;
37- const plan = await prepareCronOwnerWriteRefusal ( { storePath : "/tmp/cron.json" } , commitDeps ) ;
38- commitDeps . loadLegacyCronRepairState . mockResolvedValueOnce ( state ( [ { id : "ownerless" } ] ) ) ;
39- await expect ( plan . recheck ( ) ) . rejects . toThrow ( "ownerless legacy cron job" ) ;
88+ const corrupt = deps ( activeGateway ) ;
89+ corrupt . loadLegacyCronRepairState . mockResolvedValueOnce (
90+ state ( [ ] , [ { id : "corrupt" , reason : "invalid config" } ] ) ,
91+ ) ;
92+ await expect (
93+ prepareCronOwnerWriteRefusal ( cfg , { storePath : "/tmp/cron.json" } , corrupt ) ,
94+ ) . rejects . toThrow ( "contains 1 corrupt row" ) ;
95+ } ) ;
4096
97+ it ( "keeps include-owned binding writes fail closed" , ( ) => {
4198 expect ( ( ) =>
4299 assertAutomaticBindingsWriteAllowed ( {
43100 bindingsIncludeOwned : true ,
@@ -49,13 +106,24 @@ it("refuses unsafe ownership writes and rechecks at commit", async () => {
49106it ( "materializes a proven retained owner before the commit recheck" , async ( ) => {
50107 for ( const safe of [ deps ( ) , deps ( undefined , [ { id : "owned" , agentId : "research" } ] ) ] ) {
51108 await prepareCronOwnerWriteRefusal (
109+ cfg ,
52110 { storePath : "/tmp/cron.json" , provenOwnerAgentId : "ops" } ,
53111 safe ,
54112 ) ;
55113 expect ( safe . materializeLegacyDefaultCronJobOwners ) . not . toHaveBeenCalled ( ) ;
56114 }
57115
58116 const injected = deps ( undefined , [ { id : "ownerless" } , { id : "owned" , agentId : "research" } ] ) ;
117+ injected . loadLegacyCronRepairState . mockResolvedValueOnce (
118+ state (
119+ [ { id : "ownerless" } , { id : "owned" , agentId : "research" } ] ,
120+ [ ] ,
121+ new Map ( [
122+ [ "ownerless" , { kind : "runtime-default" as const , agentId : "ops" } ] ,
123+ [ "owned" , { kind : "explicit" as const , agentId : "research" } ] ,
124+ ] ) ,
125+ ) ,
126+ ) ;
59127 injected . materializeLegacyDefaultCronJobOwners . mockImplementationOnce ( ( ) => {
60128 injected . loadLegacyCronRepairState . mockResolvedValue (
61129 state ( [
@@ -67,6 +135,7 @@ it("materializes a proven retained owner before the commit recheck", async () =>
67135 } ) ;
68136
69137 const plan = await prepareCronOwnerWriteRefusal (
138+ cfg ,
70139 {
71140 storePath : "/tmp/custom-cron.json" ,
72141 provenOwnerAgentId : "ops" ,
@@ -87,7 +156,7 @@ it("materializes a proven retained owner before the commit recheck", async () =>
87156it ( "keeps ambiguous and failed owner handoffs as typed refusals" , async ( ) => {
88157 const ambiguous = deps ( undefined , [ { id : "ownerless" } ] ) ;
89158 await expect (
90- prepareCronOwnerWriteRefusal ( { storePath : "/tmp/cron.json" } , ambiguous ) ,
159+ prepareCronOwnerWriteRefusal ( cfg , { storePath : "/tmp/cron.json" } , ambiguous ) ,
91160 ) . rejects . toSatisfy ( isCronOwnerWriteRefusalError ) ;
92161 expect ( ambiguous . materializeLegacyDefaultCronJobOwners ) . not . toHaveBeenCalled ( ) ;
93162
@@ -96,6 +165,7 @@ it("keeps ambiguous and failed owner handoffs as typed refusals", async () => {
96165 throw new Error ( "database is temporarily read-only" ) ;
97166 } ) ;
98167 const failure = prepareCronOwnerWriteRefusal (
168+ cfg ,
99169 { storePath : "/tmp/cron.json" , provenOwnerAgentId : "ops" } ,
100170 failed ,
101171 ) ;
@@ -107,6 +177,7 @@ it("keeps ambiguous and failed owner handoffs as typed refusals", async () => {
107177 new Error ( "database disk image is malformed" ) ,
108178 ) ;
109179 const unreadable = prepareCronOwnerWriteRefusal (
180+ cfg ,
110181 { storePath : "/tmp/corrupt-cron.json" , provenOwnerAgentId : "ops" } ,
111182 corrupt ,
112183 ) ;
0 commit comments