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

terminalMenu.test.ts

102 lines3.1 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { DEFAULT_TERMINAL_ID } from "@t3tools/contracts";
4​
5import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
6​
7import {
8 basename,
9 nextOpenTerminalId,
10 previousLiveTerminalId,
11 resolveProjectScriptTerminalId,
12 type TerminalMenuSession,
13} from "./terminalMenu";
14​
15function makeMenuSession(input: {
16 readonly terminalId: string;
17 readonly status: TerminalMenuSession["status"];
18}): TerminalMenuSession {
19 return {
20 terminalId: input.terminalId,
21 cwd: null,
22 status: input.status,
23 hasRunningSubprocess: false,
24 displayLabel: getTerminalLabel(input.terminalId),
25 updatedAt: null,
26 };
28​
29describe("nextOpenTerminalId", () => {
30 it("allocates separate terminals on repeated visits without readable metadata", () => {
31 const first = nextOpenTerminalId({
32 listedTerminalIds: [],
33 uniqueSuffix: "783c91cc-a413-47c7-8312-c2a5a1f05e40",
34 });
35 const nextVisit = nextOpenTerminalId({
36 listedTerminalIds: [],
37 uniqueSuffix: "102315fc-ceef-45c4-b978-c4d4947d3c26",
38 });
39 expect(first).not.toBe(DEFAULT_TERMINAL_ID);
40 expect(nextVisit).not.toBe(first);
41 });
42​
43 it("avoids the mounted primary tab when the session list is still empty", () => {
44 expect(
45 nextOpenTerminalId({
46 listedTerminalIds: [],
47 activeRouteTerminalId: DEFAULT_TERMINAL_ID,
48 }),
49 ).toBe("term-2");
50 });
51});
52​
53describe("previousLiveTerminalId", () => {
54 it("prefers the nearest live session below the exited id", () => {
55 expect(
56 previousLiveTerminalId({
57 sessions: [
58 makeMenuSession({ terminalId: DEFAULT_TERMINAL_ID, status: "running" }),
59 makeMenuSession({ terminalId: "term-2", status: "running" }),
60 makeMenuSession({ terminalId: "term-3", status: "exited" }),
61 makeMenuSession({ terminalId: "term-4", status: "running" }),
62 ],
63 exitedTerminalId: "term-3",
64 }),
65 ).toBe("term-2");
66 });
67​
68 it("falls back to the nearest live session above when the exited id was lowest", () => {
69 expect(
70 previousLiveTerminalId({
71 sessions: [
72 makeMenuSession({ terminalId: DEFAULT_TERMINAL_ID, status: "exited" }),
73 makeMenuSession({ terminalId: "term-2", status: "starting" }),
74 makeMenuSession({ terminalId: "term-4", status: "running" }),
75 ],
76 exitedTerminalId: DEFAULT_TERMINAL_ID,
77 }),
78 ).toBe("term-2");
79 });
80});
81​
82describe("resolveProjectScriptTerminalId", () => {
83 it("never targets an unseen default terminal when metadata is unavailable", () => {
84 const terminalId = resolveProjectScriptTerminalId({
85 existingTerminalIds: [],
86 hasRunningTerminal: false,
87 uniqueSuffix: "783c91cc-a413-47c7-8312-c2a5a1f05e40",
88 });
89 expect(terminalId).not.toBe(DEFAULT_TERMINAL_ID);
90 });
91});
92​
93describe("basename", () => {
94 it.each([
95 ["C:\\wt\\repo\\", "repo"],
96 ["/home/u/repo/", "repo"],
97 ["/home/u/team\\repo", "team\\repo"],
98 ["/", "/"],
99 ])("names %s as %s", (input, expected) => {
100 expect(basename(input)).toBe(expected);
101 });
102});