Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadHistoryController.test.ts

66 lines2.3 KB

1import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5​
6import {
7 ThreadHistoryController,
8 threadHistoryControllerLayer,
9 type ThreadHistoryHandler,
10 type ThreadHistoryLoadEarlierResult,
11} from "./threadHistoryController.ts";
12​
13const ENV = "env-history" as EnvironmentId;
14const THREAD = "thread-history" as ThreadId;
15​
16function handler(
17 tag: string,
18): ThreadHistoryHandler & { readonly tag: string; readonly calls: number } {
19 const state = { tag, calls: 0 };
20 return {
21 get tag() {
22 return state.tag;
23 },
24 get calls() {
25 return state.calls;
26 },
27 loadEarlier: () => {
28 state.calls += 1;
29 return Effect.succeed({ _tag: "loaded" } satisfies ThreadHistoryLoadEarlierResult);
30 },
31 };
33​
34describe("ThreadHistoryController", () => {
35 it.effect("does not let an older finalizer delete a newer registration", () =>
36 Effect.gen(function* () {
37 const controller = yield* ThreadHistoryController;
38 const older = handler("older");
39 const newer = handler("newer");
40​
41 const olderRegistration = yield* controller.register(ENV, THREAD, older);
42 const newerRegistration = yield* controller.register(ENV, THREAD, newer);
43​
44 // Older fiber finalizes after the newer handler is already registered.
45 yield* controller.unregister(olderRegistration);
46​
47 const result = yield* controller.loadEarlier(ENV, THREAD);
48 expect(result).toEqual({ _tag: "loaded" });
49 expect(newer.calls).toBe(1);
50 expect(older.calls).toBe(0);
51​
52 yield* controller.unregister(newerRegistration);
53 expect(yield* controller.loadEarlier(ENV, THREAD)).toEqual({ _tag: "noop" });
54 }).pipe(Effect.provide(threadHistoryControllerLayer)),
55 );
56​
57 it.effect("unregister removes only its own matching registration", () =>
58 Effect.gen(function* () {
59 const controller = yield* ThreadHistoryController;
60 const first = handler("first");
61 const registration = yield* controller.register(ENV, THREAD, first);
62 yield* controller.unregister(registration);
63 expect(yield* controller.loadEarlier(ENV, THREAD)).toEqual({ _tag: "noop" });
64 }).pipe(Effect.provide(Layer.fresh(threadHistoryControllerLayer))),
65 );
66});