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
216
217
|
import { cleanup, fireEvent, render, screen } from "@testing-library/svelte";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { PendingInput } from "../pending-inputs";
import { createChatStore } from "../store.svelte";
import {
createFakeCache,
createFakeHistorySync,
createFakeMetricsSync,
createFakeTransport,
} from "../test-helpers";
import PendingInputs from "./PendingInputs.svelte";
function input(requestId: string, type: "chat.send" | "chat.queue" = "chat.send"): PendingInput {
return {
request: {
...(type === "chat.send" ? { type, message: requestId } : { type, text: requestId }),
conversationId: "c",
requestId,
images: [{ url: "data:image/png;base64,AAAA", mimeType: "image/png" }],
},
};
}
beforeEach(() => vi.useFakeTimers());
afterEach(() => {
cleanup();
const remaining = vi.getTimerCount();
vi.useRealTimers();
expect(remaining).toBe(0);
});
describe("pending input warning grace period", () => {
it.each([
"chat.send",
"chat.queue",
] as const)("renders no %s recovery UI before 1000 ms, then keeps its content stable", async (type) => {
const { container } = render(PendingInputs, {
inputs: [input("saved message", type)],
onDiscard: vi.fn(),
});
expect(container.children).toHaveLength(0);
expect(container.textContent).toBe("");
await vi.advanceTimersByTimeAsync(999);
expect(container.children).toHaveLength(0);
expect(container.textContent).toBe("");
expect(screen.queryByRole("status")).toBeNull();
await vi.advanceTimersByTimeAsync(1);
const warning = screen.getByRole("status");
expect(warning).toHaveTextContent("Delivery unconfirmed. Your input is saved on this device.");
expect(warning).toHaveTextContent(
"Check the conversation history before sending again to avoid a duplicate.",
);
expect(screen.getByRole("textbox", { name: "Saved message" })).toHaveValue("saved message");
expect(screen.getByRole("img", { name: "Saved attachment" })).toHaveAttribute(
"src",
"data:image/png;base64,AAAA",
);
const content = warning.innerHTML;
await vi.advanceTimersByTimeAsync(10_000);
expect(screen.getByRole("status")).toBe(warning);
expect(warning.innerHTML).toBe(content);
});
it.each([
"confirmation",
"discard",
])("%s before the threshold cancels the timer and never flashes later", async (resolution) => {
const transport = createFakeTransport();
const save = vi.fn();
const store = createChatStore({
conversationId: "c",
createRequestId: () => "first",
transport: transport.impl,
historySync: createFakeHistorySync().impl,
metricsSync: createFakeMetricsSync().impl,
cache: createFakeCache().impl,
pendingInputStorage: { load: () => [], save },
});
try {
store.send("first");
// Saving and transmission stay immediate, independent of warning display.
expect(save).toHaveBeenCalledExactlyOnceWith(store.pendingInputs);
expect(transport.sent).toHaveLength(1);
const { container, rerender } = render(PendingInputs, {
inputs: store.pendingInputs,
onDiscard: store.discardPendingInput,
});
await vi.advanceTimersByTimeAsync(999);
if (resolution === "confirmation") {
store.handleDelta({ type: "chat.accepted", conversationId: "c", requestId: "first" });
} else {
store.discardPendingInput("first");
}
expect(store.pendingInputs).toEqual([]);
expect(save).toHaveBeenLastCalledWith([]);
await rerender({ inputs: store.pendingInputs });
expect(vi.getTimerCount()).toBe(0);
expect(container.children).toHaveLength(0);
expect(container.textContent).toBe("");
await vi.advanceTimersByTimeAsync(10_000);
expect(container.children).toHaveLength(0);
expect(container.textContent).toBe("");
} finally {
store.dispose();
}
});
it("hides an already visible warning immediately on confirmation", async () => {
const { container, rerender } = render(PendingInputs, {
inputs: [input("first")],
onDiscard: vi.fn(),
});
await vi.advanceTimersByTimeAsync(1000);
expect(screen.getByRole("status")).toBeInTheDocument();
await rerender({ inputs: [] });
expect(container.children).toHaveLength(0);
expect(container.textContent).toBe("");
});
it("preserves the discard action and hides the saved copy without another delay", async () => {
const onDiscard = vi.fn();
const { container, rerender } = render(PendingInputs, {
inputs: [input("first")],
onDiscard,
});
await vi.advanceTimersByTimeAsync(1000);
await fireEvent.click(screen.getByRole("button", { name: "Discard saved copy" }));
expect(onDiscard).toHaveBeenCalledExactlyOnceWith("first");
await rerender({ inputs: [] });
expect(container.children).toHaveLength(0);
expect(container.textContent).toBe("");
});
it("cancels all waiting timers on unmount", async () => {
const { unmount } = render(PendingInputs, {
inputs: [input("first"), input("second")],
onDiscard: vi.fn(),
});
expect(vi.getTimerCount()).toBe(2);
await vi.advanceTimersByTimeAsync(500);
unmount();
expect(vi.getTimerCount()).toBe(0);
await vi.advanceTimersByTimeAsync(10_000);
expect(screen.queryByRole("status")).toBeNull();
});
it("gives successive inputs independent delays without restarting existing rows", async () => {
const first = input("first");
const second = input("second");
const { rerender } = render(PendingInputs, { inputs: [first], onDiscard: vi.fn() });
await vi.advanceTimersByTimeAsync(600);
await rerender({ inputs: [second, { ...first }], onDiscard: vi.fn() });
await vi.advanceTimersByTimeAsync(399);
expect(screen.queryByRole("status")).toBeNull();
await vi.advanceTimersByTimeAsync(1);
const warning = screen.getByRole("status");
expect(screen.getByRole("textbox")).toHaveValue("first");
await rerender({ inputs: [{ ...second }, { ...first }], onDiscard: vi.fn() });
await vi.advanceTimersByTimeAsync(599);
expect(screen.getAllByRole("status")).toEqual([warning]);
await vi.advanceTimersByTimeAsync(1);
expect(
screen.getAllByRole("textbox").map((node) => (node as HTMLTextAreaElement).value),
).toEqual(["second", "first"]);
// Removing the older warning must not re-hide/restart the newer warning.
await rerender({ inputs: [second] });
expect(screen.getByRole("textbox")).toHaveValue("second");
expect(vi.getTimerCount()).toBe(0);
});
it("starts a fresh delay after an earlier input was confirmed during its grace period", async () => {
const { rerender } = render(PendingInputs, { inputs: [input("first")], onDiscard: vi.fn() });
await vi.advanceTimersByTimeAsync(600);
await rerender({ inputs: [] });
await rerender({ inputs: [input("second")] });
await vi.advanceTimersByTimeAsync(999);
expect(screen.queryByRole("status")).toBeNull();
await vi.advanceTimersByTimeAsync(1);
expect(screen.getByRole("textbox")).toHaveValue("second");
});
it("keeps the original deadline when rejection arrives, and updates visible content immediately", async () => {
const pending = input("first");
const { rerender } = render(PendingInputs, { inputs: [pending], onDiscard: vi.fn() });
await vi.advanceTimersByTimeAsync(600);
await rerender({ inputs: [{ ...pending, rejection: "disk full" }] });
await vi.advanceTimersByTimeAsync(399);
expect(screen.queryByRole("status")).toBeNull();
await vi.advanceTimersByTimeAsync(1);
const warning = screen.getByRole("status");
expect(warning).toHaveTextContent("Message rejected: disk full");
expect(warning).not.toHaveTextContent("Delivery unconfirmed");
await rerender({ inputs: [{ ...pending, rejection: "still rejected" }] });
expect(screen.getByRole("status")).toBe(warning);
expect(warning).toHaveTextContent("Message rejected: still rejected");
expect(vi.getTimerCount()).toBe(0);
});
it.each([
undefined,
"disk full",
])("restored entries (rejection: %s) use the same grace period and retain recovery content", async (rejection) => {
const restored = input("restored saved message");
render(PendingInputs, {
inputs: [rejection === undefined ? restored : { ...restored, rejection }],
onDiscard: vi.fn(),
});
await vi.advanceTimersByTimeAsync(999);
expect(screen.queryByRole("status")).toBeNull();
await vi.advanceTimersByTimeAsync(1);
expect(screen.getByRole("status")).toHaveTextContent(
rejection === undefined ? "Delivery unconfirmed" : "Message rejected: disk full",
);
expect(screen.getByRole("textbox")).toHaveValue("restored saved message");
expect(screen.getByRole("button", { name: "Discard saved copy" })).toBeEnabled();
});
});
|