Open History to see the last commit
1import { EnvironmentId, type ThreadPullRequestLink } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 resolvePullRequestTabLink,
6 surfaceShortcutActionForKey,
7 surfaceShortcutTargetsTypingContext,
8} from "./RightPanelTabs";
9
10function shortcutEvent(
11 key: string,
12 overrides: Partial<Parameters<typeof surfaceShortcutActionForKey>[1]> = {},
13): Parameters<typeof surfaceShortcutActionForKey>[1] {
14 return {
15 key,
16 altKey: false,
17 ctrlKey: false,
18 defaultPrevented: false,
19 isComposing: false,
20 metaKey: false,
21 ...overrides,
22 };
23}
24
25describe("surface shortcuts", () => {
26 const actions = [
27 { shortcut: "B", available: true, label: "Browser" },
28 { shortcut: "D", available: false, label: "Diff" },
29 ] as const;
30
31 it("leaves modified, composing, and already-handled key events alone", () => {
32 expect(surfaceShortcutActionForKey(actions, shortcutEvent("b", { metaKey: true }))).toBeNull();
33 expect(
34 surfaceShortcutActionForKey(actions, shortcutEvent("b", { isComposing: true })),
35 ).toBeNull();
36 expect(
37 surfaceShortcutActionForKey(actions, shortcutEvent("b", { defaultPrevented: true })),
38 ).toBeNull();
39 });
40});
41
42describe("surface shortcut typing contexts", () => {
43 const makeTarget = (matches: string | null) => ({
44 closest(selectors: string) {
45 if (matches === null || !selectors.includes(matches)) return null;
46 return {};
47 },
48 });
49
50 it("treats form fields and every editable region as typing contexts", () => {
51 expect(surfaceShortcutTargetsTypingContext(makeTarget("input"))).toBe(true);
52 expect(surfaceShortcutTargetsTypingContext(makeTarget("textarea"))).toBe(true);
53 expect(surfaceShortcutTargetsTypingContext(makeTarget("select"))).toBe(true);
54 expect(surfaceShortcutTargetsTypingContext(makeTarget("[contenteditable]"))).toBe(true);
55 });
56});
57
58describe("pull request tab snapshots", () => {
59 const environmentId = EnvironmentId.make("local");
60 const link: ThreadPullRequestLink = {
61 host: "github.com",
62 repository: "acme/api",
63 number: 7,
64 url: "https://github.com/acme/api/pull/7",
65 source: "manual",
66 linkedAt: "2026-01-01T00:00:00Z",
67 stack: null,
68 snapshot: null,
69 };
70 it("keeps unknown linked state authoritative and scopes matches to environment and host", () => {
71 const threads = [{ environmentId, pullRequests: [link] }];
72 expect(resolvePullRequestTabLink(threads, environmentId, "github.com", link)).toBe(link);
73 expect(
74 resolvePullRequestTabLink(threads, EnvironmentId.make("remote"), "github.com", link),
75 ).toBeUndefined();
76 expect(
77 resolvePullRequestTabLink(threads, environmentId, "github.enterprise.test", link),
78 ).toBeUndefined();
79 });
80 it("uses the newest snapshot when several threads link the same PR", () => {
81 const snapshot = {
82 state: "merged" as const,
83 title: "API",
84 headBranch: "api",
85 baseBranch: "main",
86 isDraft: false,
87 updatedAt: null,
88 syncedAt: "2026-02-01T00:00:00Z",
89 };
90 const newer = { ...link, snapshot };
91 expect(
92 resolvePullRequestTabLink(
93 [{ environmentId, pullRequests: [link, newer] }],
94 environmentId,
95 "github.com",
96 link,
97 ),
98 ).toBe(newer);
99 });
100});