Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

KeybindingsUpdateToast.logic.test.ts

107 lines3.3 KB

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 };
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.each(["snapshot", "seed"])("reports unchanged-bindings recovery after %s", (initial) => {
63 const controller = createKeybindingsUpdateToastController({});
64 const malformed = {
65 kind: "keybindings.malformed-config",
66 message: "Expected JSON array",
67 } as const;
68 const delta = (
69 changes: Extract<ServerConfigStreamEvent, { type: "configDelta" }>["changes"],
70 ): ServerConfigStreamEvent => ({
71 version: 1,
72 type: "configDelta",
73 configVersion: "v2",
74 changes,
75 removed: [],
76 providerChanges: [],
77 });
78 if (initial === "seed") {
79 controller.seed([malformed]);
80 controller.seed([]);
81 } else
82 controller.handle({ version: 1, type: "snapshot", config: { issues: [malformed] } as never });
83 expect(controller.handle(delta({ issues: [malformed] }))).toBeNull();
84 expect(controller.handle(delta({ issues: [] }))).toEqual({ _tag: "Success" });
85 expect(controller.handle(delta({ issues: [] }))).toBeNull();
86 expect(controller.handle(delta({ issues: [malformed] }))).toEqual({
87 _tag: "InvalidConfiguration",
88 message: malformed.message,
89 });
90 expect(controller.handle(delta({ cwd: "/repo" }))).toBeNull();
91 expect(createKeybindingsUpdateToastController({}).handle(delta({ keybindings: [] }))).toEqual({
92 _tag: "Success",
93 });
94 });
95​
96 it("ignores unrelated server config notifications", () => {
97 const controller = createKeybindingsUpdateToastController({});
98​
99 expect(
100 controller.handle({
101 version: 1,
102 type: "settingsUpdated",
103 payload: { settings: {} as never },
104 }),
105 ).toBeNull();
106 });
107});