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, 06:00 UTC
| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import { describe, expect, test } from "@alchemy.run/test"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Fiber from "effect/Fiber"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | import * as Alchemist from "@/Alchemist/index.ts"; |
| 8 | import { devKeepAlive } from "../../src/Cli/exec"; |
| 9 | import { PlatformServices } from "../../src/Util/PlatformServices"; |
| 10 | |
| 11 | // The dev loop interrupts a parked run when a stack dependency changes (Node |
| 12 | // reloads in-process; Bun exits for the supervisor to respawn). In both cases a |
| 13 | // failed generation must park instead of exiting, while interruption propagates. |
| 14 | |
| 15 | /** Resolves `true` when the effect is still running (parked) after the timeout. */ |
| 16 | const parks = <A, E>(effect: Effect.Effect<A, E>) => |
| 17 | Effect.runPromise(effect.pipe(Effect.timeoutOption("50 millis"), Effect.map(Option.isNone))); |
| 18 | |
| 19 | describe("devKeepAlive", { tags: ["unit", "local"] }, () => { |
| 20 | test("passes success through untouched", () => |
| 21 | expect(Effect.runPromise(devKeepAlive(Effect.succeed(42)))).resolves.toBe(42)); |
| 22 | |
| 23 | test("parks on a typed failure instead of exiting", () => |
| 24 | expect(parks(devKeepAlive(Effect.fail(new Error("plan failed"))))).resolves.toBe(true)); |
| 25 | |
| 26 | test("parks on a defect instead of exiting", () => |
| 27 | expect( |
| 28 | parks(devKeepAlive(Effect.die(new TypeError("undefined is not an object")))), |
| 29 | ).resolves.toBe(true)); |
| 30 | |
| 31 | // A run that ends by itself with nothing but interruptions in its cause was |
| 32 | // cancelled from the inside (a provider or engine race), not by the user. |
| 33 | // It must park like any other failure; letting it propagate exited dev |
| 34 | // with a bare "Interrupted." and no hint of what went wrong (#1461). |
| 35 | test("an internal interrupt-only cause parks instead of exiting", () => |
| 36 | expect(parks(devKeepAlive(Effect.failCause(Cause.interrupt())))).resolves.toBe(true)); |
| 37 | |
| 38 | test("interruption tears down a run that is still working (Ctrl-C in dev)", () => |
| 39 | expect( |
| 40 | Effect.runPromise( |
| 41 | Effect.gen(function* () { |
| 42 | const fiber = yield* Effect.forkChild(devKeepAlive(Effect.sleep("1 second"))); |
| 43 | yield* Effect.sleep("20 millis"); |
| 44 | const exit = yield* Fiber.interrupt(fiber).pipe(Effect.andThen(Fiber.await(fiber))); |
| 45 | return exit._tag === "Failure" && Cause.hasInterruptsOnly(exit.cause); |
| 46 | }), |
| 47 | ), |
| 48 | ).resolves.toBe(true)); |
| 49 | |
| 50 | test("interruption still tears down a parked run (Ctrl-C in dev)", () => |
| 51 | expect( |
| 52 | Effect.runPromise( |
| 53 | Effect.gen(function* () { |
| 54 | const fiber = yield* Effect.forkChild(devKeepAlive(Effect.die("apply failed"))); |
| 55 | yield* Effect.sleep("20 millis"); |
| 56 | yield* Fiber.interrupt(fiber); |
| 57 | return "shut down"; |
| 58 | }), |
| 59 | ), |
| 60 | ).resolves.toBe("shut down")); |
| 61 | |
| 62 | // The real-world crash: a mid-edit save makes the stack entrypoint throw at |
| 63 | // module evaluation, so `importStack`'s dynamic import rejects (a defect). |
| 64 | // Wrapped in `devKeepAlive`, it must park until the import watcher observes |
| 65 | // the next save and interrupts this generation. |
| 66 | test("a stack module that throws at evaluation parks instead of crashing", () => |
| 67 | expect( |
| 68 | parks( |
| 69 | devKeepAlive( |
| 70 | Alchemist.importStack( |
| 71 | fileURLToPath(import.meta.resolve("./fixtures/import-stack-throws.ts")), |
| 72 | ).pipe(Effect.provide(PlatformServices)), |
| 73 | ), |
| 74 | ), |
| 75 | ).resolves.toBe(true)); |
| 76 | }); |