Skip to content
Source
Sign inSign up

threadHistoryController.test.ts

52 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 reconcilePendingWork: () => Effect.void,
28 };
30​
31describe("ThreadHistoryController", () => {
32 it.effect("does not let an older finalizer delete a newer registration", () =>
33 Effect.gen(function* () {
34 const controller = yield* ThreadHistoryController.ThreadHistoryController;
35 const older = handler("older");
36 const newer = handler("newer");
37​
38 const olderRegistration = yield* controller.register(ENV, THREAD, older);
39 const newerRegistration = yield* controller.register(ENV, THREAD, newer);
40​
41 yield* controller.unregister(olderRegistration);
42​
43 const result = yield* controller.loadEarlier(ENV, THREAD);
44 expect(result).toEqual({ _tag: "loaded" });
45 expect(newer.calls).toBe(1);
46 expect(older.calls).toBe(0);
47​
48 yield* controller.unregister(newerRegistration);
49 expect(yield* controller.loadEarlier(ENV, THREAD)).toEqual({ _tag: "noop" });
50 }).pipe(Effect.provide(ThreadHistoryController.layer)),
51 );
52});