1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
import { describe, expect, it } from "vitest";
import type { Tab, TabsState } from "./tabs";
import {
activeTab,
closeTab,
createTab,
deriveTitle,
initialState,
isStuckToEnd,
newDraft,
selectTab,
setModel,
setTitle,
} from "./tabs";
const tab = (conversationId: string, model = "default", title = "Chat"): Tab => ({
conversationId,
model,
title,
});
describe("initialState", () => {
it("returns empty draft state when no persisted state", () => {
const state = initialState();
expect(state.tabs).toEqual([]);
expect(state.activeConversationId).toBeNull();
});
it("returns persisted state when provided", () => {
const persisted: TabsState = {
tabs: [tab("c1")],
activeConversationId: "c1",
};
const state = initialState(persisted);
expect(state.tabs).toHaveLength(1);
expect(state.activeConversationId).toBe("c1");
});
});
describe("newDraft", () => {
it("sets activeConversationId to null", () => {
const state: TabsState = { tabs: [tab("c1")], activeConversationId: "c1" };
const next = newDraft(state);
expect(next.activeConversationId).toBeNull();
});
it("keeps existing tabs", () => {
const state: TabsState = { tabs: [tab("c1"), tab("c2")], activeConversationId: "c1" };
const next = newDraft(state);
expect(next.tabs).toHaveLength(2);
});
});
describe("createTab", () => {
it("appends and activates", () => {
const state = initialState();
const next = createTab(state, tab("c1"));
expect(next.tabs).toHaveLength(1);
expect(next.tabs[0]?.conversationId).toBe("c1");
expect(next.activeConversationId).toBe("c1");
});
it("does not duplicate an existing conversationId", () => {
const state: TabsState = { tabs: [tab("c1")], activeConversationId: "c1" };
const next = createTab(state, tab("c1"));
expect(next.tabs).toHaveLength(1);
});
it("activates an already-existing tab when createTab is called again", () => {
const state: TabsState = { tabs: [tab("c1"), tab("c2")], activeConversationId: "c2" };
const next = createTab(state, tab("c1"));
expect(next.activeConversationId).toBe("c1");
});
});
describe("selectTab", () => {
it("changes active", () => {
const state: TabsState = { tabs: [tab("c1"), tab("c2")], activeConversationId: "c1" };
const next = selectTab(state, "c2");
expect(next.activeConversationId).toBe("c2");
});
});
describe("closeTab", () => {
it("removes the tab", () => {
const state: TabsState = { tabs: [tab("c1"), tab("c2")], activeConversationId: "c1" };
const next = closeTab(state, "c2");
expect(next.tabs).toHaveLength(1);
expect(next.tabs[0]?.conversationId).toBe("c1");
});
it("closing the active tab activates a neighbour (previous preferred)", () => {
const state: TabsState = {
tabs: [tab("c1"), tab("c2"), tab("c3")],
activeConversationId: "c2",
};
const next = closeTab(state, "c2");
expect(next.activeConversationId).toBe("c1");
});
it("closing the first active tab activates the next", () => {
const state: TabsState = {
tabs: [tab("c1"), tab("c2"), tab("c3")],
activeConversationId: "c1",
};
const next = closeTab(state, "c1");
expect(next.activeConversationId).toBe("c2");
});
it("closing the last tab returns to draft (null active)", () => {
const state: TabsState = { tabs: [tab("c1")], activeConversationId: "c1" };
const next = closeTab(state, "c1");
expect(next.tabs).toHaveLength(0);
expect(next.activeConversationId).toBeNull();
});
it("closing a non-active tab does not change active", () => {
const state: TabsState = {
tabs: [tab("c1"), tab("c2"), tab("c3")],
activeConversationId: "c3",
};
const next = closeTab(state, "c1");
expect(next.activeConversationId).toBe("c3");
});
it("closing a non-existent tab is a no-op", () => {
const state: TabsState = { tabs: [tab("c1")], activeConversationId: "c1" };
const next = closeTab(state, "missing");
expect(next).toEqual(state);
});
});
describe("setModel", () => {
it("updates the right tab", () => {
const state: TabsState = { tabs: [tab("c1", "old"), tab("c2")], activeConversationId: "c1" };
const next = setModel(state, "c1", "new-model");
expect(next.tabs[0]?.model).toBe("new-model");
expect(next.tabs[1]?.model).toBe("default");
});
});
describe("setTitle", () => {
it("updates the right tab", () => {
const state: TabsState = { tabs: [tab("c1"), tab("c2")], activeConversationId: "c1" };
const next = setTitle(state, "c1", "Updated title");
expect(next.tabs[0]?.title).toBe("Updated title");
expect(next.tabs[1]?.title).toBe("Chat");
});
});
describe("activeTab", () => {
it("returns the active tab", () => {
const state: TabsState = { tabs: [tab("c1"), tab("c2")], activeConversationId: "c2" };
expect(activeTab(state)?.conversationId).toBe("c2");
});
it("returns null when activeConversationId is null", () => {
const state: TabsState = { tabs: [tab("c1")], activeConversationId: null };
expect(activeTab(state)).toBeNull();
});
it("returns null when active tab is not found in tabs", () => {
const state: TabsState = { tabs: [tab("c1")], activeConversationId: "missing" };
expect(activeTab(state)).toBeNull();
});
});
describe("deriveTitle", () => {
it("truncates long messages with ellipsis", () => {
const msg = "This is a very long message that should be truncated at some point";
expect(deriveTitle(msg, 20)).toBe("This is a very long \u2026");
});
it("returns full message when under max", () => {
expect(deriveTitle("Short", 40)).toBe("Short");
});
it("collapses whitespace", () => {
expect(deriveTitle(" hello world ")).toBe("hello world");
});
it("falls back to 'New chat' for empty input", () => {
expect(deriveTitle("")).toBe("New chat");
expect(deriveTitle(" ")).toBe("New chat");
});
it("uses default max of ~40 chars", () => {
const msg = "a".repeat(50);
const result = deriveTitle(msg);
expect(result).toBe(`${"a".repeat(40)}\u2026`);
});
});
describe("isStuckToEnd", () => {
it("is false when the strip does not overflow", () => {
expect(isStuckToEnd({ scrollLeft: 0, clientWidth: 500, scrollWidth: 500 })).toBe(false);
expect(isStuckToEnd({ scrollLeft: 0, clientWidth: 500, scrollWidth: 400 })).toBe(false);
});
it("is true when overflowing and scrolled to the left", () => {
expect(isStuckToEnd({ scrollLeft: 0, clientWidth: 500, scrollWidth: 1000 })).toBe(true);
});
it("is true when overflowing and scrolled to the middle", () => {
expect(isStuckToEnd({ scrollLeft: 250, clientWidth: 500, scrollWidth: 1000 })).toBe(true);
});
it("is false when overflowing but scrolled fully to the right", () => {
expect(isStuckToEnd({ scrollLeft: 500, clientWidth: 500, scrollWidth: 1000 })).toBe(false);
});
it("treats a 1px subpixel gap at the end as at-rest (epsilon)", () => {
expect(isStuckToEnd({ scrollLeft: 499, clientWidth: 500, scrollWidth: 1000 })).toBe(false);
});
});
|