Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 claimWorkspaceBasenameLookup,
5 needsWorkspaceBasenameLookup,
6 pickWorkspaceBasenameMatch,
7} from "./workspaceBasenameLookup";
8
9describe("needsWorkspaceBasenameLookup", () => {
10 it("flags bare filenames", () => {
11 expect(needsWorkspaceBasenameLookup("ChatView.tsx")).toBe(true);
12 expect(needsWorkspaceBasenameLookup("Makefile")).toBe(true);
13 });
14
15 it("leaves anything with a directory alone", () => {
16 expect(needsWorkspaceBasenameLookup("apps/web/src/components/ChatView.tsx")).toBe(false);
17 expect(needsWorkspaceBasenameLookup("apps\\web\\ChatView.tsx")).toBe(false);
18 expect(needsWorkspaceBasenameLookup(" ")).toBe(false);
19 });
20});
21
22describe("pickWorkspaceBasenameMatch", () => {
23 const entries = [
24 { path: "apps/web/src/components/ChatView.test.tsx", kind: "file" as const },
25 { path: "apps/web/src/components/ChatView.tsx", kind: "file" as const },
26 ];
27
28 it("takes the first exact filename match, not the closest fuzzy one", () => {
29 expect(pickWorkspaceBasenameMatch("ChatView.tsx", entries)).toBe(
30 "apps/web/src/components/ChatView.tsx",
31 );
32 });
33
34 it("ignores directories", () => {
35 expect(
36 pickWorkspaceBasenameMatch("components", [
37 { path: "apps/web/src/components", kind: "directory" },
38 { path: "apps/web/src/components/components", kind: "file" },
39 ]),
40 ).toBe("apps/web/src/components/components");
41 });
42
43 it("prefers the exactly-cased file over a case-only twin", () => {
44 expect(
45 pickWorkspaceBasenameMatch("foo.ts", [
46 { path: "src/Foo.ts", kind: "file" },
47 { path: "src/foo.ts", kind: "file" },
48 ]),
49 ).toBe("src/foo.ts");
50 });
51
52 it("falls back to case-insensitive when only the casing differs", () => {
53 expect(pickWorkspaceBasenameMatch("chatview.tsx", entries)).toBe(
54 "apps/web/src/components/ChatView.tsx",
55 );
56 });
57
58 it("returns null when the case-insensitive fallback is ambiguous", () => {
59 expect(
60 pickWorkspaceBasenameMatch("FOO.ts", [
61 { path: "src/Foo.ts", kind: "file" },
62 { path: "src/foo.ts", kind: "file" },
63 ]),
64 ).toBeNull();
65 });
66
67 it("returns null when nothing matches the name", () => {
68 expect(pickWorkspaceBasenameMatch("ChatView.tsx", [])).toBeNull();
69 expect(
70 pickWorkspaceBasenameMatch("ChatView.tsx", [
71 { path: "apps/web/src/components/ChatHeader.tsx", kind: "file" },
72 ]),
73 ).toBeNull();
74 });
75});
76
77describe("claimWorkspaceBasenameLookup", () => {
78 it("keeps only the newest claim, whatever order the lookups settle in", () => {
79 const first = claimWorkspaceBasenameLookup();
80 const second = claimWorkspaceBasenameLookup();
81
82 // The older lookup answering last must not reopen the panel behind the
83 // newer one.
84 expect(second()).toBe(true);
85 expect(first()).toBe(false);
86 });
87
88 it("stays valid while it is the only claim", () => {
89 const only = claimWorkspaceBasenameLookup();
90 expect(only()).toBe(true);
91 });
92});