Open History to see the last commit
1import { expect, it, vi } from 'vitest'
2import { atOracleCheckpoint, cleanupOfflineOracle } from './oracle-lifecycle'
3
4/**
5 * The offline oracle harness must retain the scenario's primary failure while
6 * reporting cleanup failures separately. A checkpoint timeout records a missed
7 * observation; it does not cancel the underlying operation. The fixed histories
8 * below call the shared harness directly, hold or fail cleanup stages, and check
9 * the reported error and release attempts after cleanup settles. They calibrate
10 * test ownership, not offline transaction or provider behavior.
11 */
12
13it(`retains a cleanup error as secondary and still releases later resources`, async () => {
14 const secondary = new Error(`cleanup failed`)
15 const warning = vi.spyOn(console, `warn`).mockImplementation(() => {})
16 const release = vi.fn()
17 try {
18 await expect(
19 cleanupOfflineOracle(
20 [
21 () => {
22 throw secondary
23 },
24 release,
25 ],
26 true,
27 ),
28 ).resolves.toBeUndefined()
29 expect(release).toHaveBeenCalledOnce()
30 expect(warning).toHaveBeenCalledWith(
31 `Offline oracle cleanup failed after the primary failure:`,
32 [secondary],
33 )
34 } finally {
35 warning.mockRestore()
36 }
37})
38
39it(`bounds a lost completion checkpoint and attempts every cleanup stage`, async () => {
40 vi.useFakeTimers()
41 const release = vi.fn()
42 const warning = vi.spyOn(console, `warn`).mockImplementation(() => {})
43 try {
44 const never = new Promise<void>(() => {})
45 const checkpoint = atOracleCheckpoint(never, `provider entered`).catch(
46 (error: unknown) => error,
47 )
48 const cleanup = cleanupOfflineOracle([() => never, release], true)
49 await vi.advanceTimersByTimeAsync(1001)
50 expect(release).toHaveBeenCalledOnce()
51 expect(await checkpoint).toMatchObject({
52 message: `Offline oracle checkpoint timed out: provider entered`,
53 })
54 await cleanup
55 expect(warning).toHaveBeenCalledOnce()
56 expect(warning).toHaveBeenCalledWith(
57 `Offline oracle cleanup failed after the primary failure:`,
58 [
59 expect.objectContaining({
60 message: `Offline oracle checkpoint timed out: cleanup stage 0`,
61 }),
62 ],
63 )
64 } finally {
65 warning.mockRestore()
66 vi.useRealTimers()
67 }
68})
69
70it(`keeps the scenario failure when cleanup also fails`, async () => {
71 const primary = new Error(`scenario failed`)
72 const firstCleanupFailure = new Error(`first cleanup failed`)
73 const secondCleanupFailure = new Error(`second cleanup failed`)
74 const warning = vi.spyOn(console, `warn`).mockImplementation(() => {})
75 const release = vi.fn()
76 try {
77 const scenario = async () => {
78 try {
79 throw primary
80 } finally {
81 await cleanupOfflineOracle(
82 [
83 () => {
84 throw firstCleanupFailure
85 },
86 () => {
87 throw secondCleanupFailure
88 },
89 release,
90 ],
91 true,
92 )
93 }
94 }
95
96 await expect(scenario()).rejects.toBe(primary)
97 expect(release).toHaveBeenCalledOnce()
98 expect(warning).toHaveBeenCalledWith(
99 `Offline oracle cleanup failed after the primary failure:`,
100 [firstCleanupFailure, secondCleanupFailure],
101 )
102 } finally {
103 warning.mockRestore()
104 }
105})
106
107it(`reports cleanup-only failures after attempting later resources`, async () => {
108 const failure = new Error(`cleanup failed`)
109 const release = vi.fn()
110 await expect(
111 cleanupOfflineOracle(
112 [
113 () => {
114 throw failure
115 },
116 release,
117 ],
118 false,
119 ),
120 ).rejects.toMatchObject({ errors: [failure] })
121 expect(release).toHaveBeenCalledOnce()
122})