1import type { ServerConfigStreamEvent } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 createKeybindingsUpdateToastController,
6 KEYBINDINGS_SUCCESS_TOAST_COOLDOWN_MS,
7} from "./KeybindingsUpdateToast.logic";
8
9function keybindingsEvent(
10 overrides: Partial<Extract<ServerConfigStreamEvent, { type: "keybindingsUpdated" }>> = {},
11): Extract<ServerConfigStreamEvent, { type: "keybindingsUpdated" }> {
12 return {
13 version: 1,
14 type: "keybindingsUpdated",
15 payload: {
16 keybindings: [],
17 issues: [],
18 },
19 ...overrides,
20 };
21}
22
23describe("keybindings update toast policy", () => {
24 it("coalesces repeated successful reload notifications during the cooldown", () => {
25 let now = 1_000;
26 const controller = createKeybindingsUpdateToastController({
27 now: () => now,
28 });
29
30 expect(controller.handle(keybindingsEvent())).toEqual({ _tag: "Success" });
31
32 now += KEYBINDINGS_SUCCESS_TOAST_COOLDOWN_MS - 1;
33 expect(controller.handle(keybindingsEvent())).toBeNull();
34
35 now += 1;
36 expect(controller.handle(keybindingsEvent())).toEqual({ _tag: "Success" });
37 });
38
39 it("surfaces keybinding configuration issues", () => {
40 const controller = createKeybindingsUpdateToastController({});
41
42 expect(
43 controller.handle(
44 keybindingsEvent({
45 payload: {
46 keybindings: [],
47 issues: [
48 {
49 kind: "keybindings.malformed-config",
50 message: "Expected JSON array",
51 },
52 ],
53 },
54 }),
55 ),
56 ).toEqual({
57 _tag: "InvalidConfiguration",
58 message: "Expected JSON array",
59 });
60 });
61
62 it("ignores unrelated server config notifications", () => {
63 const controller = createKeybindingsUpdateToastController({});
64
65 expect(
66 controller.handle({
67 version: 1,
68 type: "settingsUpdated",
69 payload: { settings: {} as never },
70 }),
71 ).toBeNull();
72 });
73});