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

threadHistoryController.test.ts

51 lines1.7 KB

1import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4​
5import * as ThreadHistoryController from "./threadHistoryController.ts";
6​
7const ENV = "env-history" as EnvironmentId;
8const THREAD = "thread-history" as ThreadId;
9​
10function handler(
11 tag: string,
12): ThreadHistoryController.ThreadHistoryHandler & { readonly tag: string; readonly calls: number } {
13 const state = { tag, calls: 0 };
14 return {
15 get tag() {
16 return state.tag;
17 },
18 get calls() {
19 return state.calls;
20 },
21 loadEarlier: () => {
22 state.calls += 1;
23 return Effect.succeed({
24 _tag: "loaded",
25 } satisfies ThreadHistoryController.ThreadHistoryLoadEarlierResult);
26 },
27 };
29​
30describe("ThreadHistoryController", () => {
31 it.effect("does not let an older finalizer delete a newer registration", () =>
32 Effect.gen(function* () {
33 const controller = yield* ThreadHistoryController.ThreadHistoryController;
34 const older = handler("older");
35 const newer = handler("newer");
36​
37 const olderRegistration = yield* controller.register(ENV, THREAD, older);
38 const newerRegistration = yield* controller.register(ENV, THREAD, newer);
39​
40 yield* controller.unregister(olderRegistration);
41​
42 const result = yield* controller.loadEarlier(ENV, THREAD);
43 expect(result).toEqual({ _tag: "loaded" });
44 expect(newer.calls).toBe(1);
45 expect(older.calls).toBe(0);
46​
47 yield* controller.unregister(newerRegistration);
48 expect(yield* controller.loadEarlier(ENV, THREAD)).toEqual({ _tag: "noop" });
49 }).pipe(Effect.provide(ThreadHistoryController.layer)),
50 );
51});