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

workspaceBasenameLookup.test.ts

63 lines2.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 claimWorkspaceBasenameLookup,
5 pickWorkspaceBasenameMatch,
6} from "@t3tools/client-runtime/workspace-basename-lookup";
7​
8describe("pickWorkspaceBasenameMatch", () => {
9 const entries = [
10 { path: "apps/web/src/components/ChatView.test.tsx", kind: "file" as const },
11 { path: "apps/web/src/components/ChatView.tsx", kind: "file" as const },
12 ];
13​
14 it("takes the first exact filename match, not the closest fuzzy one", () => {
15 expect(pickWorkspaceBasenameMatch("ChatView.tsx", entries)).toBe(
16 "apps/web/src/components/ChatView.tsx",
17 );
18 });
19​
20 // regression: #6297 a bare filename resolves to a file, never a same-named directory
21 it("ignores directories", () => {
22 expect(
23 pickWorkspaceBasenameMatch("components", [
24 { path: "apps/web/src/components", kind: "directory" },
25 { path: "apps/web/src/components/components", kind: "file" },
26 ]),
27 ).toBe("apps/web/src/components/components");
28 });
29​
30 it("prefers the exactly-cased file over a case-only twin", () => {
31 expect(
32 pickWorkspaceBasenameMatch("foo.ts", [
33 { path: "src/Foo.ts", kind: "file" },
34 { path: "src/foo.ts", kind: "file" },
35 ]),
36 ).toBe("src/foo.ts");
37 });
38​
39 it("falls back to case-insensitive when only the casing differs", () => {
40 expect(pickWorkspaceBasenameMatch("chatview.tsx", entries)).toBe(
41 "apps/web/src/components/ChatView.tsx",
42 );
43 });
44​
45 it("returns null when the case-insensitive fallback is ambiguous", () => {
46 expect(
47 pickWorkspaceBasenameMatch("FOO.ts", [
48 { path: "src/Foo.ts", kind: "file" },
49 { path: "src/foo.ts", kind: "file" },
50 ]),
51 ).toBeNull();
52 });
53});
54​
55describe("claimWorkspaceBasenameLookup", () => {
56 it("keeps only the newest claim, whatever order the lookups settle in", () => {
57 const first = claimWorkspaceBasenameLookup();
58 const second = claimWorkspaceBasenameLookup();
59​
60 expect(second()).toBe(true);
61 expect(first()).toBe(false);
62 });
63});