forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecker.test.ts
More file actions
134 lines (117 loc) · 4.87 KB
/
Copy pathchecker.test.ts
File metadata and controls
134 lines (117 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
// ─── Fixtures ─────────────────────────────────────────────────────────────────
const { fsMock } = vi.hoisted(() => ({
fsMock: {
existsSync: vi.fn(),
readFileSync: vi.fn(),
writeFileSync: vi.fn(),
statSync: vi.fn(),
},
}));
vi.mock("node:fs", () => fsMock);
// ─── Tests ────────────────────────────────────────────────────────────────────
describe("isLocalDevMode / getLocalDevPath", () => {
beforeEach(() => {
vi.resetAllMocks();
fsMock.existsSync.mockReturnValue(false);
});
afterEach(() => {
vi.restoreAllMocks();
});
it("returns false when no config files exist", async () => {
const { isLocalDevMode } = await import("./checker");
expect(isLocalDevMode("/some/project")).toBe(false);
});
it("returns null from getLocalDevPath when no config exists", async () => {
const { getLocalDevPath } = await import("./checker");
expect(getLocalDevPath("/some/project")).toBeNull();
});
it("returns null when config has no matching file:// plugin entry", async () => {
const { getLocalDevPath } = await import("./checker");
fsMock.existsSync.mockImplementation((p: string) =>
p.endsWith("opencode.json"),
);
fsMock.readFileSync.mockReturnValue(
JSON.stringify({ plugin: ["some-other-plugin@1.0.0"] }),
);
expect(getLocalDevPath("/project")).toBeNull();
});
it("returns path when config contains a file:// entry for the package", async () => {
const { getLocalDevPath } = await import("./checker");
fsMock.existsSync.mockImplementation((p: string) =>
p.endsWith("opencode.json"),
);
fsMock.readFileSync.mockReturnValue(
JSON.stringify({
plugin: ["file:///home/user/opencode-antigravity-auth/dist/plugin.js"],
}),
);
const result = getLocalDevPath("/project");
expect(result).toContain("opencode-antigravity-auth");
});
it("handles JSONC config with comments and trailing commas", async () => {
const { getLocalDevPath } = await import("./checker");
fsMock.existsSync.mockImplementation((p: string) =>
p.endsWith("opencode.jsonc"),
);
fsMock.readFileSync.mockReturnValue(
`{
// dev plugin
"plugin": [
"file:///home/user/opencode-antigravity-auth/dist/plugin.js",
]
}`,
);
const result = getLocalDevPath("/project");
expect(result).toContain("opencode-antigravity-auth");
});
it("returns null and does not throw when config file is malformed JSON", async () => {
const { getLocalDevPath } = await import("./checker");
fsMock.existsSync.mockReturnValue(true);
fsMock.readFileSync.mockReturnValue("{ not valid json !!!}");
expect(() => getLocalDevPath("/project")).not.toThrow();
expect(getLocalDevPath("/project")).toBeNull();
});
});
describe("findPluginEntry", () => {
beforeEach(() => {
vi.resetAllMocks();
fsMock.existsSync.mockReturnValue(false);
});
it("returns null when no config files exist", async () => {
const { findPluginEntry } = await import("./checker");
expect(findPluginEntry("/project")).toBeNull();
});
it("returns entry with isPinned=false for bare package name", async () => {
const { findPluginEntry } = await import("./checker");
fsMock.existsSync.mockImplementation((p: string) => p.endsWith("opencode.json"));
fsMock.readFileSync.mockReturnValue(
JSON.stringify({ plugin: ["opencode-antigravity-auth"] }),
);
const result = findPluginEntry("/project");
expect(result).not.toBeNull();
expect(result!.isPinned).toBe(false);
expect(result!.pinnedVersion).toBeNull();
});
it("returns entry with isPinned=true for versioned package", async () => {
const { findPluginEntry } = await import("./checker");
fsMock.existsSync.mockImplementation((p: string) => p.endsWith("opencode.json"));
fsMock.readFileSync.mockReturnValue(
JSON.stringify({ plugin: ["opencode-antigravity-auth@1.5.0"] }),
);
const result = findPluginEntry("/project");
expect(result).not.toBeNull();
expect(result!.isPinned).toBe(true);
expect(result!.pinnedVersion).toBe("1.5.0");
});
it("returns isPinned=false for @latest entry", async () => {
const { findPluginEntry } = await import("./checker");
fsMock.existsSync.mockImplementation((p: string) => p.endsWith("opencode.json"));
fsMock.readFileSync.mockReturnValue(
JSON.stringify({ plugin: ["opencode-antigravity-auth@latest"] }),
);
const result = findPluginEntry("/project");
expect(result!.isPinned).toBe(false);
expect(result!.pinnedVersion).toBeNull();
});
});