1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
3import { describe, expect, it } from "vite-plus/test";
4
5import { registerSettleAndAdvance, settleAndAdvance } from "./settleAndAdvance";
6
7const threadRef = scopeThreadRef(
8 EnvironmentId.make("environment-local"),
9 ThreadId.make("thread-1"),
10);
11
12describe("settleAndAdvance", () => {
13 it("hands the open thread to the mounted sidebar so it can move forward", () => {
14 const settled: ScopedThreadRef[] = [];
15 const unregister = registerSettleAndAdvance((ref) => settled.push(ref));
16
17 expect(settleAndAdvance(threadRef)).toBe(true);
18 expect(settled).toEqual([threadRef]);
19 unregister();
20 });
21
22 it("asks the caller to settle in place when no sidebar is mounted", () => {
23 expect(settleAndAdvance(threadRef)).toBe(false);
24 });
25
26 it("keeps a remounted sidebar's handler when the old one unmounts late", () => {
27 const settledBy: string[] = [];
28 const unregisterOld = registerSettleAndAdvance(() => settledBy.push("old"));
29 const unregisterNew = registerSettleAndAdvance(() => settledBy.push("new"));
30 unregisterOld();
31
32 expect(settleAndAdvance(threadRef)).toBe(true);
33 expect(settledBy).toEqual(["new"]);
34 unregisterNew();
35 expect(settleAndAdvance(threadRef)).toBe(false);
36 });
37});