Open History to see the last commit
1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { assert, describe, it } from "vite-plus/test";
3import {
4 buildVisibleToastLayout,
5 shouldHideCollapsedToastContent,
6 shouldRenderThreadScopedToast,
7} from "./toast.logic";
8
9describe("shouldHideCollapsedToastContent", () => {
10 it("keeps a single visible toast readable", () => {
11 assert.equal(shouldHideCollapsedToastContent(0, 1), false);
12 });
13
14 it("keeps the front-most toast readable in a visible stack", () => {
15 assert.equal(shouldHideCollapsedToastContent(0, 3), false);
16 });
17
18 it("hides non-front toasts until the stack is expanded", () => {
19 assert.equal(shouldHideCollapsedToastContent(1, 3), true);
20 });
21});
22
23describe("buildVisibleToastLayout", () => {
24 it("computes indices and offsets from the visible subset", () => {
25 const visibleToasts = [
26 { id: "a", height: 48 },
27 { id: "b", height: 72 },
28 { id: "c", height: 24 },
29 ];
30
31 const layout = buildVisibleToastLayout(visibleToasts);
32
33 assert.equal(layout.frontmostHeight, 48);
34 assert.deepEqual(
35 layout.items.map(({ toast, visibleIndex, offsetY }) => ({
36 id: toast.id,
37 visibleIndex,
38 offsetY,
39 })),
40 [
41 { id: "a", visibleIndex: 0, offsetY: 0 },
42 { id: "b", visibleIndex: 1, offsetY: 48 },
43 { id: "c", visibleIndex: 2, offsetY: 120 },
44 ],
45 );
46 });
47
48 it("reflows live toasts forward when the front toast is dismissed", () => {
49 const visibleToasts = [
50 { id: "a", height: 48, transitionStatus: "ending" as const },
51 { id: "b", height: 72 },
52 { id: "c", height: 24 },
53 ];
54
55 const layout = buildVisibleToastLayout(visibleToasts);
56
57 // frontmost height should be the first live toast, not the ending one
58 assert.equal(layout.frontmostHeight, 72);
59 assert.deepEqual(
60 layout.items.map(({ toast, visibleIndex, offsetY }) => ({
61 id: toast.id,
62 visibleIndex,
63 offsetY,
64 })),
65 [
66 // Ending toast stays at its front slot; data-ending-style drives its exit
67 { id: "a", visibleIndex: 0, offsetY: 0 },
68 // Live toasts get fresh indices starting at 0 so they move up in sync
69 { id: "b", visibleIndex: 0, offsetY: 0 },
70 { id: "c", visibleIndex: 1, offsetY: 72 },
71 ],
72 );
73 });
74
75 it("keeps a non-front ending toast at its current slot so it exits straight", () => {
76 const visibleToasts = [
77 { id: "a", height: 48 },
78 { id: "b", height: 72, transitionStatus: "ending" as const },
79 { id: "c", height: 24 },
80 ];
81
82 const layout = buildVisibleToastLayout(visibleToasts);
83
84 // front toast stays, so frontmost height is unchanged
85 assert.equal(layout.frontmostHeight, 48);
86 assert.deepEqual(
87 layout.items.map(({ toast, visibleIndex, offsetY }) => ({
88 id: toast.id,
89 visibleIndex,
90 offsetY,
91 })),
92 [
93 // Front live toast — unaffected
94 { id: "a", visibleIndex: 0, offsetY: 0 },
95 // Ending toast keeps its pre-dismissal slot so its horizontal exit
96 // originates from where the user saw it (not from Y=0).
97 { id: "b", visibleIndex: 1, offsetY: 48 },
98 // Live toast behind "b" slides forward into the vacated slot.
99 { id: "c", visibleIndex: 1, offsetY: 48 },
100 ],
101 );
102 });
103
104 it("treats missing heights as zero", () => {
105 const layout = buildVisibleToastLayout([
106 { id: "a" },
107 { id: "b", height: undefined },
108 { id: "c", height: 30 },
109 ]);
110
111 assert.equal(layout.frontmostHeight, 0);
112 assert.deepEqual(
113 layout.items.map(({ toast, offsetY }) => ({
114 id: toast.id,
115 offsetY,
116 })),
117 [
118 { id: "a", offsetY: 0 },
119 { id: "b", offsetY: 0 },
120 { id: "c", offsetY: 0 },
121 ],
122 );
123 });
124});
125
126describe("shouldRenderThreadScopedToast", () => {
127 const activeThreadRef = {
128 environmentId: "environment-a",
129 threadId: "thread-1",
130 } as ScopedThreadRef;
131
132 it("renders a toast scoped to the active thread ref", () => {
133 assert.equal(
134 shouldRenderThreadScopedToast(
135 {
136 threadRef: activeThreadRef,
137 },
138 activeThreadRef,
139 ),
140 true,
141 );
142 });
143
144 it("hides a scoped toast when the environment differs", () => {
145 assert.equal(
146 shouldRenderThreadScopedToast(
147 {
148 threadRef: {
149 environmentId: "environment-b",
150 threadId: "thread-1",
151 } as ScopedThreadRef,
152 },
153 activeThreadRef,
154 ),
155 false,
156 );
157 });
158
159 it("keeps legacy thread-id scoped toasts working", () => {
160 assert.equal(
161 shouldRenderThreadScopedToast(
162 {
163 threadId: "thread-1" as never,
164 },
165 activeThreadRef,
166 ),
167 true,
168 );
169 });
170});