The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { afterAll, assert, beforeAll, beforeEach, describe, it, vi } from "@effect/vitest" |
| 2 | import { Effect, Layer } from "effect" |
| 3 | |
| 4 | const pendingReleases: Array<Promise<void>> = [] |
| 5 | |
| 6 | // Drain the real timers even when a broken adapter abandons a finalizer. |
| 7 | afterAll(() => Promise.all(pendingReleases)) |
| 8 | |
| 9 | // The release takes real time: the TestClock is not advanced once a test has timed out. |
| 10 | const resource = (events: Array<string>, name: string) => |
| 11 | Effect.acquireRelease( |
| 12 | Effect.sync(() => { |
| 13 | events.push(`${name}:acquired`) |
| 14 | }), |
| 15 | () => |
| 16 | Effect.promise(() => { |
| 17 | events.push(`${name}:release:start`) |
| 18 | const pending = new Promise<void>((resolve) => |
| 19 | setTimeout(() => { |
| 20 | events.push(`${name}:release:end`) |
| 21 | resolve() |
| 22 | }, 250) |
| 23 | ) |
| 24 | pendingReleases.push(pending) |
| 25 | return pending |
| 26 | }) |
| 27 | ) |
| 28 | |
| 29 | for (const mode of ["effect", "live"] as const) { |
| 30 | describe(`it.${mode} timeout finalizers`, { concurrent: false }, () => { |
| 31 | const events: Array<string> = [] |
| 32 | let signal: AbortSignal | undefined |
| 33 | |
| 34 | it[mode].fails("times out while holding a resource", (ctx) => |
| 35 | Effect.gen(function*() { |
| 36 | signal = ctx.signal |
| 37 | yield* resource(events, "test") |
| 38 | return yield* Effect.never |
| 39 | }), { timeout: 75 }) |
| 40 | |
| 41 | it("awaits the finalizer before starting the next test", () => { |
| 42 | events.push("next:start") |
| 43 | assert.isTrue(signal?.aborted) |
| 44 | assert.deepStrictEqual(events, ["test:acquired", "test:release:start", "test:release:end", "next:start"]) |
| 45 | }) |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | describe("it.effect timeout finalizer defects", { concurrent: false }, () => { |
| 50 | const logged: Array<string> = [] |
| 51 | const collect = (...args: Array<unknown>) => { |
| 52 | logged.push(args.map(String).join(" ")) |
| 53 | } |
| 54 | // The default logger writes to console.log, or console.error when logging to stderr. |
| 55 | beforeAll(() => { |
| 56 | vi.spyOn(console, "log").mockImplementation(collect) |
| 57 | vi.spyOn(console, "error").mockImplementation(collect) |
| 58 | }) |
| 59 | afterAll(() => vi.restoreAllMocks()) |
| 60 | |
| 61 | it.effect.fails("times out while a finalizer dies", () => |
| 62 | Effect.gen(function*() { |
| 63 | yield* Effect.addFinalizer(() => Effect.die(new Error("finalizer failed"))) |
| 64 | return yield* Effect.never |
| 65 | }), { timeout: 75 }) |
| 66 | |
| 67 | it("reports the finalizer defect", () => { |
| 68 | assert.isTrue(logged.some((line) => line.includes("finalizer failed"))) |
| 69 | }) |
| 70 | }) |
| 71 | |
| 72 | describe("beforeEach timeout finalizers", { concurrent: false }, () => { |
| 73 | const events: Array<string> = [] |
| 74 | |
| 75 | describe("hook", () => { |
| 76 | beforeEach(() => |
| 77 | Effect.gen(function*() { |
| 78 | yield* resource(events, "hook") |
| 79 | return yield* Effect.never |
| 80 | }), 75) |
| 81 | |
| 82 | it.fails("times out while holding a resource", () => {}) |
| 83 | }) |
| 84 | |
| 85 | it("awaits the finalizer before starting the next test", () => { |
| 86 | events.push("next:start") |
| 87 | assert.deepStrictEqual(events, ["hook:acquired", "hook:release:start", "hook:release:end", "next:start"]) |
| 88 | }) |
| 89 | }) |
| 90 | |
| 91 | describe("beforeEach teardown timeout finalizers", { concurrent: false }, () => { |
| 92 | const events: Array<string> = [] |
| 93 | |
| 94 | describe("hook", () => { |
| 95 | beforeEach(() => resource(events, "hook"), 75) |
| 96 | |
| 97 | it.fails("times out while releasing the resource", () => {}) |
| 98 | }) |
| 99 | |
| 100 | it("awaits the finalizer before starting the next test", () => { |
| 101 | events.push("next:start") |
| 102 | assert.deepStrictEqual(events, ["hook:acquired", "hook:release:start", "hook:release:end", "next:start"]) |
| 103 | }) |
| 104 | }) |
| 105 | |
| 106 | // Vitest fails a suite whose beforeAll fails, so accept the expected error. |
| 107 | // The suite is the second argument; Vitest requires the first to be destructured. |
| 108 | const expectBeforeAllFailure = (message: string) => |
| 109 | // oxlint-disable-next-line no-empty-pattern |
| 110 | afterAll(({}, suite) => { |
| 111 | assert.include(suite.result?.errors?.map((error) => error.message).join("\n"), message) |
| 112 | suite.result!.state = "pass" |
| 113 | suite.result!.errors = [] |
| 114 | }) |
| 115 | |
| 116 | describe("beforeAll failure finalizers", { concurrent: false }, () => { |
| 117 | const events: Array<string> = [] |
| 118 | |
| 119 | describe("failing hook", () => { |
| 120 | beforeAll(() => resource(events, "failed").pipe(Effect.andThen(Effect.fail(new Error("boom"))))) |
| 121 | expectBeforeAllFailure("boom") |
| 122 | |
| 123 | it("is skipped", () => {}) |
| 124 | }) |
| 125 | |
| 126 | // The hook's timeout also bounds the disposal, so it must fit the release. |
| 127 | describe("hook succeeding after its timeout", () => { |
| 128 | beforeAll(() => resource(events, "late").pipe(Effect.andThen(Effect.sleep(500))), 400) |
| 129 | expectBeforeAllFailure("timed out") |
| 130 | |
| 131 | it("is skipped", () => {}) |
| 132 | }) |
| 133 | |
| 134 | describe("hook succeeding after its timeout, before the cleanups", () => { |
| 135 | // Delays the cleanups until the setup has succeeded and Vitest has dropped its cleanup. |
| 136 | afterAll(() => new Promise((resolve) => setTimeout(resolve, 150))) |
| 137 | beforeAll(() => resource(events, "dropped").pipe(Effect.andThen(Effect.sleep(500))), 400) |
| 138 | expectBeforeAllFailure("timed out") |
| 139 | |
| 140 | it("is skipped", () => {}) |
| 141 | }) |
| 142 | |
| 143 | it("awaits the finalizers before starting the next test", () => { |
| 144 | events.push("next:start") |
| 145 | assert.deepStrictEqual(events, [ |
| 146 | "failed:acquired", |
| 147 | "failed:release:start", |
| 148 | "failed:release:end", |
| 149 | "late:acquired", |
| 150 | "late:release:start", |
| 151 | "late:release:end", |
| 152 | "dropped:acquired", |
| 153 | "dropped:release:start", |
| 154 | "dropped:release:end", |
| 155 | "next:start" |
| 156 | ]) |
| 157 | }) |
| 158 | }) |
| 159 | |
| 160 | // Registers hooks while the default hook timeout is shorter than the release. |
| 161 | const withShortDefaultHookTimeout = (register: () => void) => { |
| 162 | vi.setConfig({ hookTimeout: 100 }) |
| 163 | register() |
| 164 | vi.resetConfig() |
| 165 | } |
| 166 | |
| 167 | for (const hooks of ["stack", "list", "parallel"] as const) { |
| 168 | describe(`beforeAll teardown with ${hooks} hooks`, { concurrent: false }, () => { |
| 169 | const events: Array<string> = [] |
| 170 | |
| 171 | // Vitest reads the hook order when it runs hooks and cleanups. |
| 172 | beforeAll(() => vi.setConfig({ sequence: { hooks } })) |
| 173 | afterAll(() => vi.resetConfig()) |
| 174 | |
| 175 | describe("hook followed by an earlier afterAll", () => { |
| 176 | afterAll(() => { |
| 177 | events.push("afterAll") |
| 178 | }) |
| 179 | beforeAll(() => resource(events, "ordered").pipe(Effect.as(() => events.push("ordered:cleanup")))) |
| 180 | |
| 181 | it("uses the resource", () => {}) |
| 182 | }) |
| 183 | |
| 184 | describe("hook with a timeout longer than the default", () => { |
| 185 | withShortDefaultHookTimeout(() => beforeAll(() => resource(events, "explicit"), 1000)) |
| 186 | |
| 187 | it("uses the resource", () => {}) |
| 188 | }) |
| 189 | |
| 190 | describe("hook whose nested tests skip at runtime", () => { |
| 191 | withShortDefaultHookTimeout(() => beforeAll(() => resource(events, "skipped"), 1000)) |
| 192 | |
| 193 | describe("nested", () => { |
| 194 | it("skips at runtime", (ctx) => ctx.skip()) |
| 195 | }) |
| 196 | }) |
| 197 | |
| 198 | describe("hook followed by a failing beforeAll", () => { |
| 199 | withShortDefaultHookTimeout(() => beforeAll(() => resource(events, "kept"), 1000)) |
| 200 | beforeAll(() => { |
| 201 | throw new Error("later setup failed") |
| 202 | }) |
| 203 | expectBeforeAllFailure("later setup failed") |
| 204 | |
| 205 | it("is skipped", () => {}) |
| 206 | }) |
| 207 | |
| 208 | it("releases after the suite's afterAll hooks, within the hook's timeout", () => { |
| 209 | events.push("next:start") |
| 210 | assert.deepStrictEqual(events, [ |
| 211 | "ordered:acquired", |
| 212 | "afterAll", |
| 213 | "ordered:cleanup", |
| 214 | "ordered:release:start", |
| 215 | "ordered:release:end", |
| 216 | "explicit:acquired", |
| 217 | "explicit:release:start", |
| 218 | "explicit:release:end", |
| 219 | "skipped:acquired", |
| 220 | "skipped:release:start", |
| 221 | "skipped:release:end", |
| 222 | "kept:acquired", |
| 223 | "kept:release:start", |
| 224 | "kept:release:end", |
| 225 | "next:start" |
| 226 | ]) |
| 227 | }) |
| 228 | }) |
| 229 | } |
| 230 | |
| 231 | describe("it.effect timeout retry finalizers", { concurrent: false }, () => { |
| 232 | const events: Array<string> = [] |
| 233 | const abortedAtStart: Array<boolean> = [] |
| 234 | let attempts = 0 |
| 235 | |
| 236 | it.effect("retries after timing out while holding a resource", (ctx) => { |
| 237 | const attempt = ++attempts |
| 238 | abortedAtStart.push(ctx.signal.aborted) |
| 239 | if (attempt === 2) { |
| 240 | // The retry inherits an aborted signal. Keep its assertion uninterruptible. |
| 241 | return Effect.sync(() => { |
| 242 | events.push("retry:start") |
| 243 | assert.deepStrictEqual(events, [ |
| 244 | "attempt1:acquired", |
| 245 | "attempt1:release:start", |
| 246 | "attempt1:release:end", |
| 247 | "retry:start" |
| 248 | ]) |
| 249 | }).pipe(Effect.uninterruptible) |
| 250 | } |
| 251 | return Effect.gen(function*() { |
| 252 | yield* resource(events, "attempt1") |
| 253 | return yield* Effect.never |
| 254 | }) |
| 255 | }, { timeout: 75, retry: 1 }) |
| 256 | |
| 257 | it("completes a genuine retry before starting the next test", () => { |
| 258 | events.push("next:start") |
| 259 | assert.strictEqual(attempts, 2) |
| 260 | // Vitest reuses the signal aborted by the first attempt's timeout. |
| 261 | assert.deepStrictEqual(abortedAtStart, [false, true]) |
| 262 | assert.deepStrictEqual(events, [ |
| 263 | "attempt1:acquired", |
| 264 | "attempt1:release:start", |
| 265 | "attempt1:release:end", |
| 266 | "retry:start", |
| 267 | "next:start" |
| 268 | ]) |
| 269 | }) |
| 270 | }) |
| 271 | |
| 272 | describe("it.layer timeout finalizers", { concurrent: false, retry: 0, repeats: 0 }, () => { |
| 273 | const events: Array<string> = [] |
| 274 | let signal: AbortSignal | undefined |
| 275 | |
| 276 | it.layer(Layer.effectDiscard(resource(events, "layer")))((it) => { |
| 277 | it.effect.fails("times out in the last test using the layer", (ctx) => |
| 278 | Effect.gen(function*() { |
| 279 | signal = ctx.signal |
| 280 | yield* resource(events, "test") |
| 281 | return yield* Effect.never |
| 282 | }), { timeout: 75 }) |
| 283 | }) |
| 284 | |
| 285 | it("awaits both test and layer finalizers before starting the next test", () => { |
| 286 | events.push("next:start") |
| 287 | assert.isTrue(signal?.aborted) |
| 288 | assert.deepStrictEqual(events, [ |
| 289 | "layer:acquired", |
| 290 | "test:acquired", |
| 291 | "test:release:start", |
| 292 | "test:release:end", |
| 293 | "layer:release:start", |
| 294 | "layer:release:end", |
| 295 | "next:start" |
| 296 | ]) |
| 297 | }) |
| 298 | }) |