Open History to see the last commit
1/**
2 * Harness self-test for the persisted oracle's cleanup helper: every cleanup action runs, and a primary failure is not hidden. The cleanup law is owned by persisted-oracle.test.ts through cleanupPersistedOracle; this file is an ordinary test of that machinery.
3 */
4import { expect, it, vi } from 'vitest'
5import { cleanupPersistedOracle } from './persisted-support'
6
7it.each([false, true])(
8 `persisted oracle cleanup attempts every action without hiding a primary failure: %s`,
9 async (hasPrimaryFailure) => {
10 const firstFailure = new Error(`first oracle cleanup failure`)
11 const secondFailure = new Error(`second oracle cleanup failure`)
12 const attempts: Array<string> = []
13 const warning = vi.spyOn(console, `warn`).mockImplementation(() => {})
14
15 try {
16 const outcome = await cleanupPersistedOracle(
17 [
18 () => {
19 attempts.push(`first`)
20 throw firstFailure
21 },
22 () => {
23 attempts.push(`second`)
24 return Promise.reject(secondFailure)
25 },
26 () => {
27 attempts.push(`last`)
28 },
29 ],
30 hasPrimaryFailure,
31 ).then(
32 () => undefined,
33 (error: unknown) => error,
34 )
35
36 expect(attempts).toEqual([`first`, `second`, `last`])
37 if (hasPrimaryFailure) {
38 expect(outcome).toBeUndefined()
39 expect(warning).toHaveBeenCalledWith(
40 `Persisted oracle cleanup failed after the primary failure:`,
41 [firstFailure, secondFailure],
42 )
43 } else {
44 expect(outcome).toBeInstanceOf(AggregateError)
45 expect((outcome as AggregateError).errors).toEqual([
46 firstFailure,
47 secondFailure,
48 ])
49 expect(warning).not.toHaveBeenCalled()
50 }
51 } finally {
52 warning.mockRestore()
53 }
54 },
55)