The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Fiber from "effect/Fiber"; |
| 4 | import * as Logger from "effect/Logger"; |
| 5 | import { MinimumLogLevel } from "effect/References"; |
| 6 | import { emit, onEvent, type Event } from "@/Events.ts"; |
| 7 | import { |
| 8 | type LogRecord, |
| 9 | logChildLine, |
| 10 | parseRecord, |
| 11 | recordOf, |
| 12 | replayRecord, |
| 13 | } from "@/Util/LogRecord.ts"; |
| 14 | import { processTrace } from "@/Util/TraceParent.ts"; |
| 15 | |
| 16 | const capture = () => { |
| 17 | const records: Array<LogRecord> = []; |
| 18 | return { |
| 19 | records, |
| 20 | layer: Logger.layer([ |
| 21 | Logger.make<unknown, void>((options) => void records.push(recordOf(options))), |
| 22 | ]), |
| 23 | }; |
| 24 | }; |
| 25 | |
| 26 | const phase = (phase: "loading-state" | "computing-plan"): Event => ({ |
| 27 | type: "plan.phase", |
| 28 | phase, |
| 29 | }); |
| 30 | |
| 31 | describe("log records", { tags: ["unit", "local"] }, () => { |
| 32 | it.effect("never throws on values JSON cannot encode", () => { |
| 33 | const { records, layer } = capture(); |
| 34 | const cyclic: Record<string, unknown> = { big: 1n }; |
| 35 | cyclic.self = cyclic; |
| 36 | return Effect.logInfo(cyclic).pipe( |
| 37 | Effect.provide(layer), |
| 38 | Effect.andThen( |
| 39 | Effect.sync(() => |
| 40 | expect(records[0]).toMatchObject({ |
| 41 | kind: "log", |
| 42 | data: { message: '{"big":"1","self":"[Circular]"}' }, |
| 43 | }), |
| 44 | ), |
| 45 | ), |
| 46 | ); |
| 47 | }); |
| 48 | |
| 49 | it.effect("replays a record with its original time and cause", () => { |
| 50 | const { records, layer } = capture(); |
| 51 | const record = { |
| 52 | time: 1767225600000, |
| 53 | level: "error", |
| 54 | kind: "log", |
| 55 | process: "sidecar", |
| 56 | scope: "global", |
| 57 | trace: { traceId: "t1", spanId: "s1" }, |
| 58 | attributes: { fqn: "Api", provider: "Cloudflare" }, |
| 59 | data: { message: "boom", cause: "Error: boom\n at x" }, |
| 60 | } as const; |
| 61 | return replayRecord(record).pipe( |
| 62 | Effect.provide(layer), |
| 63 | Effect.andThen(Effect.sync(() => expect(records[0]).toEqual(record))), |
| 64 | ); |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | describe("events", { tags: ["unit", "local"] }, () => { |
| 69 | it.effect("reach subscribers whatever the minimum log level", () => { |
| 70 | const seen: Array<string> = []; |
| 71 | return emit(phase("loading-state")).pipe( |
| 72 | onEvent((event) => Effect.sync(() => void seen.push(event.type))), |
| 73 | Effect.provideService(MinimumLogLevel, "Error"), |
| 74 | Effect.andThen(Effect.sync(() => expect(seen).toEqual(["plan.phase"]))), |
| 75 | ); |
| 76 | }); |
| 77 | |
| 78 | it.live("are handled in order and before onEvent returns", () => { |
| 79 | const seen: Array<string> = []; |
| 80 | return Effect.gen(function* () { |
| 81 | yield* emit(phase("loading-state")); |
| 82 | yield* emit(phase("computing-plan")); |
| 83 | }).pipe( |
| 84 | onEvent((event) => |
| 85 | event.type === "plan.phase" && event.phase === "loading-state" |
| 86 | ? Effect.sleep("20 millis").pipe( |
| 87 | Effect.andThen(Effect.sync(() => void seen.push("first"))), |
| 88 | ) |
| 89 | : Effect.sync(() => void seen.push("second")), |
| 90 | ), |
| 91 | Effect.andThen(Effect.sync(() => expect(seen).toEqual(["first", "second"]))), |
| 92 | ); |
| 93 | }); |
| 94 | |
| 95 | it.live("stop a blocked handler when the operation is interrupted", () => |
| 96 | Effect.gen(function* () { |
| 97 | const exit = yield* emit(phase("loading-state")).pipe( |
| 98 | Effect.andThen(Effect.never), |
| 99 | onEvent(() => Effect.never), |
| 100 | Effect.timeout("50 millis"), |
| 101 | Effect.exit, |
| 102 | ); |
| 103 | expect(exit._tag).toBe("Failure"); |
| 104 | }), |
| 105 | ); |
| 106 | |
| 107 | it.live("fail the operation when a handler dies", () => |
| 108 | Effect.gen(function* () { |
| 109 | const exit = yield* emit(phase("loading-state")).pipe( |
| 110 | onEvent(() => Effect.die("handler broke")), |
| 111 | Effect.exit, |
| 112 | ); |
| 113 | expect(exit._tag).toBe("Failure"); |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | it.live("keep the observed effect on the caller's fiber", () => |
| 118 | Effect.gen(function* () { |
| 119 | let ticks = 0; |
| 120 | const child = yield* Effect.forkChild( |
| 121 | Effect.forever( |
| 122 | Effect.sync(() => void ticks++).pipe(Effect.andThen(Effect.sleep("1 millis"))), |
| 123 | ), |
| 124 | ).pipe(onEvent(() => Effect.void)); |
| 125 | yield* Effect.sleep("30 millis"); |
| 126 | // A fiber forked inside `onEvent` outlives it, as it would without it. |
| 127 | expect(ticks).toBeGreaterThan(1); |
| 128 | yield* Fiber.interrupt(child); |
| 129 | }), |
| 130 | ); |
| 131 | |
| 132 | it.live("reject events emitted through captured loggers after cancellation", () => |
| 133 | Effect.gen(function* () { |
| 134 | const seen: Array<string> = []; |
| 135 | let captured: ReadonlySet<Logger.Logger<unknown, any>> = new Set(); |
| 136 | yield* Effect.gen(function* () { |
| 137 | captured = yield* Logger.CurrentLoggers; |
| 138 | yield* Effect.never; |
| 139 | }).pipe( |
| 140 | onEvent((event) => Effect.sync(() => void seen.push(event.type))), |
| 141 | Effect.timeout("20 millis"), |
| 142 | Effect.exit, |
| 143 | ); |
| 144 | // An apply session that captured the subscriber keeps emitting. |
| 145 | yield* emit(phase("loading-state")).pipe( |
| 146 | Effect.provideService(Logger.CurrentLoggers, captured), |
| 147 | ); |
| 148 | yield* Effect.sleep("20 millis"); |
| 149 | expect(seen).toEqual([]); |
| 150 | }), |
| 151 | ); |
| 152 | }); |
| 153 | |
| 154 | describe("trace", { tags: ["unit", "local"] }, () => { |
| 155 | it.effect("is the current span inside one and the process trace outside", () => { |
| 156 | const { records, layer } = capture(); |
| 157 | return Effect.gen(function* () { |
| 158 | yield* Effect.logInfo("outside"); |
| 159 | yield* Effect.logInfo("inside").pipe(Effect.withSpan("work")); |
| 160 | const span = yield* Effect.withSpan(Effect.currentSpan, "check"); |
| 161 | expect(records[0]!.trace).toEqual(processTrace); |
| 162 | expect(records[1]!.trace.traceId).not.toBe(processTrace.traceId); |
| 163 | expect(span.traceId).toMatch(/^[0-9a-f]+$/); |
| 164 | }).pipe(Effect.provide(layer)); |
| 165 | }); |
| 166 | }); |
| 167 | |
| 168 | describe("event records", { tags: ["unit", "local"] }, () => { |
| 169 | const line = (data: object) => |
| 170 | JSON.stringify({ |
| 171 | time: 0, |
| 172 | level: "info", |
| 173 | kind: "event", |
| 174 | process: "exec", |
| 175 | scope: "project", |
| 176 | trace: { traceId: "t", spanId: "s" }, |
| 177 | attributes: {}, |
| 178 | data, |
| 179 | }); |
| 180 | |
| 181 | it("decodes a known event in full", () => { |
| 182 | expect(parseRecord(line({ type: "plan.phase", phase: "loading-state" }))?.data).toEqual({ |
| 183 | type: "plan.phase", |
| 184 | phase: "loading-state", |
| 185 | }); |
| 186 | }); |
| 187 | |
| 188 | it("rejects a known event with the wrong shape", () => { |
| 189 | expect(parseRecord(line({ type: "plan.phase", phase: 42 }))).toBeUndefined(); |
| 190 | }); |
| 191 | |
| 192 | it("keeps an event of a type it doesn't know", () => { |
| 193 | expect(parseRecord(line({ type: "deploy.future", detail: 1 }))?.data).toEqual({ |
| 194 | type: "deploy.future", |
| 195 | detail: 1, |
| 196 | }); |
| 197 | }); |
| 198 | }); |