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

terminalMenu.test.ts

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