@perpetualsystems/typescript-sdk 0.0.0-bootstrap.0 → 0.4.0-next-20261110214241
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -1
- package/dist/authorization-code-pkce.d.ts +40 -0
- package/dist/authorization-code-pkce.d.ts.map +1 -0
- package/dist/authorization-code-pkce.js +272 -0
- package/dist/authorization-code-pkce.js.map +1 -0
- package/dist/client-credentials.d.ts +18 -0
- package/dist/client-credentials.d.ts.map +1 -0
- package/dist/client-credentials.js +79 -0
- package/dist/client-credentials.js.map +1 -0
- package/dist/client-store.d.ts +37 -0
- package/dist/client-store.d.ts.map +1 -0
- package/dist/client-store.js +138 -0
- package/dist/client-store.js.map +1 -0
- package/dist/client.d.ts +113 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +217 -0
- package/dist/client.js.map +1 -0
- package/dist/config-dir.d.ts +3 -0
- package/dist/config-dir.d.ts.map +1 -0
- package/dist/config-dir.js +11 -0
- package/dist/config-dir.js.map +1 -0
- package/dist/dynamic-registration.d.ts +28 -0
- package/dist/dynamic-registration.d.ts.map +1 -0
- package/dist/dynamic-registration.js +56 -0
- package/dist/dynamic-registration.js.map +1 -0
- package/dist/generated/client/client.gen.d.ts +3 -0
- package/dist/generated/client/client.gen.d.ts.map +1 -0
- package/dist/generated/client/client.gen.js +217 -0
- package/dist/generated/client/client.gen.js.map +1 -0
- package/dist/generated/client/index.d.ts +11 -0
- package/dist/generated/client/index.d.ts.map +1 -0
- package/dist/generated/client/index.js +7 -0
- package/dist/generated/client/index.js.map +1 -0
- package/dist/generated/client/types.gen.d.ts +121 -0
- package/dist/generated/client/types.gen.d.ts.map +1 -0
- package/dist/generated/client/types.gen.js +3 -0
- package/dist/generated/client/types.gen.js.map +1 -0
- package/dist/generated/client/utils.gen.d.ts +38 -0
- package/dist/generated/client/utils.gen.d.ts.map +1 -0
- package/dist/generated/client/utils.gen.js +229 -0
- package/dist/generated/client/utils.gen.js.map +1 -0
- package/dist/generated/client.gen.d.ts +13 -0
- package/dist/generated/client.gen.d.ts.map +1 -0
- package/dist/generated/client.gen.js +4 -0
- package/dist/generated/client.gen.js.map +1 -0
- package/dist/generated/core/auth.gen.d.ts +26 -0
- package/dist/generated/core/auth.gen.d.ts.map +1 -0
- package/dist/generated/core/auth.gen.js +15 -0
- package/dist/generated/core/auth.gen.js.map +1 -0
- package/dist/generated/core/bodySerializer.gen.d.ts +26 -0
- package/dist/generated/core/bodySerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/bodySerializer.gen.js +58 -0
- package/dist/generated/core/bodySerializer.gen.js.map +1 -0
- package/dist/generated/core/params.gen.d.ts +44 -0
- package/dist/generated/core/params.gen.d.ts.map +1 -0
- package/dist/generated/core/params.gen.js +110 -0
- package/dist/generated/core/params.gen.js.map +1 -0
- package/dist/generated/core/pathSerializer.gen.d.ts +34 -0
- package/dist/generated/core/pathSerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/pathSerializer.gen.js +107 -0
- package/dist/generated/core/pathSerializer.gen.js.map +1 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts +19 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/queryKeySerializer.gen.js +93 -0
- package/dist/generated/core/queryKeySerializer.gen.js.map +1 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts +72 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts.map +1 -0
- package/dist/generated/core/serverSentEvents.gen.js +133 -0
- package/dist/generated/core/serverSentEvents.gen.js.map +1 -0
- package/dist/generated/core/types.gen.d.ts +84 -0
- package/dist/generated/core/types.gen.d.ts.map +1 -0
- package/dist/generated/core/types.gen.js +3 -0
- package/dist/generated/core/types.gen.js.map +1 -0
- package/dist/generated/core/utils.gen.d.ts +20 -0
- package/dist/generated/core/utils.gen.d.ts.map +1 -0
- package/dist/generated/core/utils.gen.js +88 -0
- package/dist/generated/core/utils.gen.js.map +1 -0
- package/dist/generated/enum-values.gen.d.ts +331 -0
- package/dist/generated/enum-values.gen.d.ts.map +1 -0
- package/dist/generated/enum-values.gen.js +332 -0
- package/dist/generated/enum-values.gen.js.map +1 -0
- package/dist/generated/file-schemas.gen.d.ts +14 -0
- package/dist/generated/file-schemas.gen.d.ts.map +1 -0
- package/dist/generated/file-schemas.gen.js +3786 -0
- package/dist/generated/file-schemas.gen.js.map +1 -0
- package/dist/generated/index.d.ts +3 -0
- package/dist/generated/index.d.ts.map +1 -0
- package/dist/generated/index.js +3 -0
- package/dist/generated/index.js.map +1 -0
- package/dist/generated/sdk.gen.d.ts +1713 -0
- package/dist/generated/sdk.gen.d.ts.map +1 -0
- package/dist/generated/sdk.gen.js +10574 -0
- package/dist/generated/sdk.gen.js.map +1 -0
- package/dist/generated/spec-source.gen.d.ts +8 -0
- package/dist/generated/spec-source.gen.d.ts.map +1 -0
- package/dist/generated/spec-source.gen.js +9 -0
- package/dist/generated/spec-source.gen.js.map +1 -0
- package/dist/generated/types.gen.d.ts +30382 -0
- package/dist/generated/types.gen.d.ts.map +1 -0
- package/dist/generated/types.gen.js +3 -0
- package/dist/generated/types.gen.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/oauth-request-error.d.ts +26 -0
- package/dist/oauth-request-error.d.ts.map +1 -0
- package/dist/oauth-request-error.js +47 -0
- package/dist/oauth-request-error.js.map +1 -0
- package/dist/refresh-token.d.ts +50 -0
- package/dist/refresh-token.d.ts.map +1 -0
- package/dist/refresh-token.js +83 -0
- package/dist/refresh-token.js.map +1 -0
- package/dist/token-store.d.ts +38 -0
- package/dist/token-store.d.ts.map +1 -0
- package/dist/token-store.js +195 -0
- package/dist/token-store.js.map +1 -0
- package/openapi/openapi.json +128387 -0
- package/package.json +66 -4
- package/src/__tests__/authorization-code-pkce.test.ts +245 -0
- package/src/__tests__/client-credentials.test.ts +360 -0
- package/src/__tests__/client-store.test.ts +139 -0
- package/src/__tests__/client.test.ts +529 -0
- package/src/__tests__/config-dir.test.ts +22 -0
- package/src/__tests__/dynamic-registration.test.ts +181 -0
- package/src/__tests__/oauth-request-error.test.ts +148 -0
- package/src/__tests__/refresh-token.test.ts +206 -0
- package/src/__tests__/token-store.test.ts +449 -0
- package/src/authorization-code-pkce.ts +417 -0
- package/src/client-credentials.ts +127 -0
- package/src/client-store.ts +182 -0
- package/src/client.ts +261 -0
- package/src/config-dir.ts +12 -0
- package/src/dynamic-registration.ts +109 -0
- package/src/generated/client/client.gen.ts +277 -0
- package/src/generated/client/index.ts +27 -0
- package/src/generated/client/types.gen.ts +218 -0
- package/src/generated/client/utils.gen.ts +316 -0
- package/src/generated/client.gen.ts +16 -0
- package/src/generated/core/auth.gen.ts +48 -0
- package/src/generated/core/bodySerializer.gen.ts +82 -0
- package/src/generated/core/params.gen.ts +178 -0
- package/src/generated/core/pathSerializer.gen.ts +171 -0
- package/src/generated/core/queryKeySerializer.gen.ts +117 -0
- package/src/generated/core/serverSentEvents.gen.ts +242 -0
- package/src/generated/core/types.gen.ts +110 -0
- package/src/generated/core/utils.gen.ts +140 -0
- package/src/generated/enum-values.gen.ts +332 -0
- package/src/generated/file-schemas.gen.ts +3800 -0
- package/src/generated/index.ts +4 -0
- package/src/generated/sdk.gen.ts +10992 -0
- package/src/generated/spec-source.gen.ts +9 -0
- package/src/generated/types.gen.ts +32415 -0
- package/src/index.ts +55 -0
- package/src/oauth-request-error.ts +75 -0
- package/src/refresh-token.ts +142 -0
- package/src/token-store.ts +252 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chmodSync,
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
mkdtempSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
statSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from "node:fs";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
clearAllTokens,
|
|
17
|
+
clearToken,
|
|
18
|
+
getStoredToken,
|
|
19
|
+
normalizeEndpoint,
|
|
20
|
+
saveToken,
|
|
21
|
+
} from "../token-store.js";
|
|
22
|
+
|
|
23
|
+
describe("token-store", () => {
|
|
24
|
+
let tempDir: string;
|
|
25
|
+
const originalEnv = process.env.XDG_CONFIG_HOME;
|
|
26
|
+
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
tempDir = mkdtempSync(join(tmpdir(), "perpetual-test-"));
|
|
29
|
+
// Point XDG_CONFIG_HOME to temp dir; token-store will use <tempDir>/perpetual/
|
|
30
|
+
process.env.XDG_CONFIG_HOME = tempDir;
|
|
31
|
+
// Pre-create the perpetual subdirectory for tests that write files directly
|
|
32
|
+
mkdirSync(join(tempDir, "perpetual"), { recursive: true });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
if (originalEnv === undefined) {
|
|
37
|
+
delete process.env.XDG_CONFIG_HOME;
|
|
38
|
+
} else {
|
|
39
|
+
process.env.XDG_CONFIG_HOME = originalEnv;
|
|
40
|
+
}
|
|
41
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("normalizeEndpoint", () => {
|
|
45
|
+
it("strips trailing slash", () => {
|
|
46
|
+
expect(normalizeEndpoint("https://app.prptl.org/")).toBe(
|
|
47
|
+
"https://app.prptl.org",
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("lowercases the hostname", () => {
|
|
52
|
+
expect(normalizeEndpoint("https://APP.PRPTL.ORG")).toBe(
|
|
53
|
+
"https://app.prptl.org",
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("strips multiple trailing slashes", () => {
|
|
58
|
+
expect(normalizeEndpoint("https://app.prptl.org///")).toBe(
|
|
59
|
+
"https://app.prptl.org",
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("preserves path segments", () => {
|
|
64
|
+
expect(normalizeEndpoint("https://app.prptl.org/v2")).toBe(
|
|
65
|
+
"https://app.prptl.org/v2",
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("preserves port", () => {
|
|
70
|
+
expect(normalizeEndpoint("https://localhost:3000/")).toBe(
|
|
71
|
+
"https://localhost:3000",
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("throws on plain domain without protocol", () => {
|
|
76
|
+
expect(() => normalizeEndpoint("not-a-url")).toThrow(
|
|
77
|
+
'Invalid endpoint URL: "not-a-url"',
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("includes parse error detail in throw message", () => {
|
|
82
|
+
expect(() => normalizeEndpoint("not-a-url")).toThrow("Parse error:");
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe("getStoredToken", () => {
|
|
87
|
+
it("returns null when no token file exists", () => {
|
|
88
|
+
expect(getStoredToken("https://app.prptl.org")).toBeNull();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("returns null for corrupt JSON file and creates backup", () => {
|
|
92
|
+
writeFileSync(join(tempDir, "perpetual", "tokens.json"), "not json{{{");
|
|
93
|
+
const stderrSpy = vi
|
|
94
|
+
.spyOn(process.stderr, "write")
|
|
95
|
+
.mockImplementation(() => true);
|
|
96
|
+
|
|
97
|
+
expect(getStoredToken("https://app.prptl.org")).toBeNull();
|
|
98
|
+
expect(stderrSpy).toHaveBeenCalledWith(
|
|
99
|
+
expect.stringContaining("tokens.json contains invalid JSON"),
|
|
100
|
+
);
|
|
101
|
+
// Verify backup was created
|
|
102
|
+
expect(existsSync(join(tempDir, "perpetual", "tokens.json.bak"))).toBe(
|
|
103
|
+
true,
|
|
104
|
+
);
|
|
105
|
+
expect(
|
|
106
|
+
readFileSync(join(tempDir, "perpetual", "tokens.json.bak"), "utf-8"),
|
|
107
|
+
).toBe("not json{{{");
|
|
108
|
+
|
|
109
|
+
stderrSpy.mockRestore();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("returns null for non-object JSON and creates backup", () => {
|
|
113
|
+
writeFileSync(
|
|
114
|
+
join(tempDir, "perpetual", "tokens.json"),
|
|
115
|
+
'"just a string"',
|
|
116
|
+
);
|
|
117
|
+
const stderrSpy = vi
|
|
118
|
+
.spyOn(process.stderr, "write")
|
|
119
|
+
.mockImplementation(() => true);
|
|
120
|
+
|
|
121
|
+
expect(getStoredToken("https://app.prptl.org")).toBeNull();
|
|
122
|
+
expect(stderrSpy).toHaveBeenCalledWith(
|
|
123
|
+
expect.stringContaining("unexpected format"),
|
|
124
|
+
);
|
|
125
|
+
// Verify backup was created
|
|
126
|
+
expect(existsSync(join(tempDir, "perpetual", "tokens.json.bak"))).toBe(
|
|
127
|
+
true,
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
stderrSpy.mockRestore();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("treats array JSON as corrupt: returns null, warns, and backs it up", () => {
|
|
134
|
+
writeFileSync(join(tempDir, "perpetual", "tokens.json"), "[1,2,3]");
|
|
135
|
+
const stderrSpy = vi
|
|
136
|
+
.spyOn(process.stderr, "write")
|
|
137
|
+
.mockImplementation(() => true);
|
|
138
|
+
|
|
139
|
+
expect(getStoredToken("https://app.prptl.org")).toBeNull();
|
|
140
|
+
expect(stderrSpy).toHaveBeenCalledWith(
|
|
141
|
+
"Warning: tokens.json has unexpected format, ignoring\n",
|
|
142
|
+
);
|
|
143
|
+
expect(
|
|
144
|
+
readFileSync(join(tempDir, "perpetual", "tokens.json.bak"), "utf-8"),
|
|
145
|
+
).toBe("[1,2,3]");
|
|
146
|
+
|
|
147
|
+
stderrSpy.mockRestore();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("returns null for endpoint not in file", () => {
|
|
151
|
+
writeFileSync(
|
|
152
|
+
join(tempDir, "perpetual", "tokens.json"),
|
|
153
|
+
JSON.stringify({ "https://other.com": { token: "abc" } }),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
expect(getStoredToken("https://app.prptl.org")).toBeNull();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("returns null when stored token is empty string", () => {
|
|
160
|
+
writeFileSync(
|
|
161
|
+
join(tempDir, "perpetual", "tokens.json"),
|
|
162
|
+
JSON.stringify({
|
|
163
|
+
"https://app.prptl.org": { token: "" },
|
|
164
|
+
}),
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
expect(getStoredToken("https://app.prptl.org")).toBeNull();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("returns null for invalid expiresAt date", () => {
|
|
171
|
+
writeFileSync(
|
|
172
|
+
join(tempDir, "perpetual", "tokens.json"),
|
|
173
|
+
JSON.stringify({
|
|
174
|
+
"https://app.prptl.org": {
|
|
175
|
+
token: "abc",
|
|
176
|
+
expiresAt: "not-a-valid-date",
|
|
177
|
+
},
|
|
178
|
+
}),
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
expect(getStoredToken("https://app.prptl.org")).toBeNull();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("returns null for expired token", () => {
|
|
185
|
+
const past = new Date(Date.now() - 60000).toISOString();
|
|
186
|
+
writeFileSync(
|
|
187
|
+
join(tempDir, "perpetual", "tokens.json"),
|
|
188
|
+
JSON.stringify({
|
|
189
|
+
"https://app.prptl.org": { token: "abc", expiresAt: past },
|
|
190
|
+
}),
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
expect(getStoredToken("https://app.prptl.org")).toBeNull();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("returns null for token expiring within 60s buffer", () => {
|
|
197
|
+
const soonExpiry = new Date(Date.now() + 30000).toISOString(); // 30 seconds from now
|
|
198
|
+
writeFileSync(
|
|
199
|
+
join(tempDir, "perpetual", "tokens.json"),
|
|
200
|
+
JSON.stringify({
|
|
201
|
+
"https://app.prptl.org": { token: "abc", expiresAt: soonExpiry },
|
|
202
|
+
}),
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
expect(getStoredToken("https://app.prptl.org")).toBeNull();
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("returns token when not expired", () => {
|
|
209
|
+
const future = new Date(Date.now() + 3600000).toISOString();
|
|
210
|
+
writeFileSync(
|
|
211
|
+
join(tempDir, "perpetual", "tokens.json"),
|
|
212
|
+
JSON.stringify({
|
|
213
|
+
"https://app.prptl.org": { token: "abc", expiresAt: future },
|
|
214
|
+
}),
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
const result = getStoredToken("https://app.prptl.org");
|
|
218
|
+
expect(result).toEqual({ token: "abc", expiresAt: future });
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("returns token without expiry", () => {
|
|
222
|
+
writeFileSync(
|
|
223
|
+
join(tempDir, "perpetual", "tokens.json"),
|
|
224
|
+
JSON.stringify({
|
|
225
|
+
"https://app.prptl.org": { token: "abc" },
|
|
226
|
+
}),
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
const result = getStoredToken("https://app.prptl.org");
|
|
230
|
+
expect(result).toEqual({ token: "abc" });
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe("saveToken", () => {
|
|
235
|
+
it("creates config dir and file if they do not exist", () => {
|
|
236
|
+
const nestedDir = join(tempDir, "custom-xdg");
|
|
237
|
+
process.env.XDG_CONFIG_HOME = nestedDir;
|
|
238
|
+
|
|
239
|
+
saveToken("https://app.prptl.org", "my-token");
|
|
240
|
+
|
|
241
|
+
const tokenFile = join(nestedDir, "perpetual", "tokens.json");
|
|
242
|
+
expect(existsSync(tokenFile)).toBe(true);
|
|
243
|
+
const content = JSON.parse(readFileSync(tokenFile, "utf-8"));
|
|
244
|
+
expect(content["https://app.prptl.org"]).toEqual({ token: "my-token" });
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("saves token with expiry", () => {
|
|
248
|
+
const expiry = "2099-01-01T00:00:00.000Z";
|
|
249
|
+
saveToken("https://app.prptl.org", "my-token", { expiresAt: expiry });
|
|
250
|
+
|
|
251
|
+
const content = JSON.parse(
|
|
252
|
+
readFileSync(join(tempDir, "perpetual", "tokens.json"), "utf-8"),
|
|
253
|
+
);
|
|
254
|
+
expect(content["https://app.prptl.org"]).toEqual({
|
|
255
|
+
token: "my-token",
|
|
256
|
+
expiresAt: expiry,
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("saves token with userId and orgId metadata", () => {
|
|
261
|
+
saveToken("https://app.prptl.org", "my-token", {
|
|
262
|
+
clientId: "cid-123",
|
|
263
|
+
userId: "USR_ABC",
|
|
264
|
+
orgId: "ORG_XYZ",
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const content = JSON.parse(
|
|
268
|
+
readFileSync(join(tempDir, "perpetual", "tokens.json"), "utf-8"),
|
|
269
|
+
);
|
|
270
|
+
expect(content["https://app.prptl.org::cid-123"]).toEqual({
|
|
271
|
+
token: "my-token",
|
|
272
|
+
userId: "USR_ABC",
|
|
273
|
+
orgId: "ORG_XYZ",
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("uses composite key with clientId", () => {
|
|
278
|
+
saveToken("https://app.prptl.org", "token-a", { clientId: "client-a" });
|
|
279
|
+
saveToken("https://app.prptl.org", "token-b", { clientId: "client-b" });
|
|
280
|
+
|
|
281
|
+
expect(getStoredToken("https://app.prptl.org", "client-a")?.token).toBe(
|
|
282
|
+
"token-a",
|
|
283
|
+
);
|
|
284
|
+
expect(getStoredToken("https://app.prptl.org", "client-b")?.token).toBe(
|
|
285
|
+
"token-b",
|
|
286
|
+
);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it("round-trips with getStoredToken", () => {
|
|
290
|
+
saveToken("https://app.prptl.org", "round-trip-token");
|
|
291
|
+
const result = getStoredToken("https://app.prptl.org");
|
|
292
|
+
expect(result).toEqual({ token: "round-trip-token" });
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("preserves tokens for other endpoints", () => {
|
|
296
|
+
saveToken("https://one.com", "token-one");
|
|
297
|
+
saveToken("https://two.com", "token-two");
|
|
298
|
+
|
|
299
|
+
expect(getStoredToken("https://one.com")?.token).toBe("token-one");
|
|
300
|
+
expect(getStoredToken("https://two.com")?.token).toBe("token-two");
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("overwrites existing token for same endpoint", () => {
|
|
304
|
+
saveToken("https://app.prptl.org", "old-token");
|
|
305
|
+
saveToken("https://app.prptl.org", "new-token");
|
|
306
|
+
|
|
307
|
+
expect(getStoredToken("https://app.prptl.org")?.token).toBe("new-token");
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("throws when saving an empty token", () => {
|
|
311
|
+
expect(() => saveToken("https://app.prptl.org", "")).toThrow(
|
|
312
|
+
"Cannot save empty token",
|
|
313
|
+
);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("throws when saving an invalid expiresAt date", () => {
|
|
317
|
+
expect(() =>
|
|
318
|
+
saveToken("https://app.prptl.org", "my-token", {
|
|
319
|
+
expiresAt: "not-a-date",
|
|
320
|
+
}),
|
|
321
|
+
).toThrow("Invalid expiresAt date: not-a-date");
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
if (process.platform !== "win32") {
|
|
325
|
+
it("sets file permissions to 0600", () => {
|
|
326
|
+
saveToken("https://app.prptl.org", "my-token");
|
|
327
|
+
const stats = statSync(join(tempDir, "perpetual", "tokens.json"));
|
|
328
|
+
// eslint-disable-next-line no-bitwise
|
|
329
|
+
expect(stats.mode & 0o777).toBe(0o600);
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (process.platform !== "win32") {
|
|
334
|
+
it("surfaces a clear error and leaves no .tmp behind when the staged write itself fails", () => {
|
|
335
|
+
// Seed real tokens.json, then revoke write permission on the
|
|
336
|
+
// perpetual/ directory so writeFileSync on the .tmp staging path
|
|
337
|
+
// fails with EACCES. saveToken's catch branch must surface a
|
|
338
|
+
// "Failed to write token file" error and not leave a half-baked
|
|
339
|
+
// .tmp behind.
|
|
340
|
+
// POSIX-only because Windows ACLs don't honor `chmod 0o500` —
|
|
341
|
+
// matches the convention at the 0600-perm test above.
|
|
342
|
+
saveToken("https://app.prptl.org", "original-token");
|
|
343
|
+
const dir = join(tempDir, "perpetual");
|
|
344
|
+
const filePath = join(dir, "tokens.json");
|
|
345
|
+
const tmpPath = `${filePath}.tmp`;
|
|
346
|
+
const originalBytes = readFileSync(filePath, "utf-8");
|
|
347
|
+
// r-x------ : read+exec, no write
|
|
348
|
+
const originalDirMode = statSync(dir).mode & 0o777;
|
|
349
|
+
chmodSync(dir, 0o500);
|
|
350
|
+
try {
|
|
351
|
+
expect(() =>
|
|
352
|
+
saveToken("https://app.prptl.org", "replacement-token"),
|
|
353
|
+
).toThrow(/Failed to write token file/);
|
|
354
|
+
} finally {
|
|
355
|
+
// Restore so afterEach cleanup can rm the temp dir.
|
|
356
|
+
chmodSync(dir, originalDirMode);
|
|
357
|
+
}
|
|
358
|
+
expect(readFileSync(filePath, "utf-8")).toBe(originalBytes);
|
|
359
|
+
expect(existsSync(tmpPath)).toBe(false);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
it("cleans up .tmp and preserves original tokens.json when renameSync throws", async () => {
|
|
364
|
+
// Directly exercises the unlinkSync(tmpPath) branch in the failure
|
|
365
|
+
// catch — the staged write succeeded (so .tmp exists on disk), then
|
|
366
|
+
// renameSync threw. Uses vi.doMock + dynamic import because vitest's
|
|
367
|
+
// ESM spyOn can't replace named-export bindings already destructured
|
|
368
|
+
// by token-store at module load.
|
|
369
|
+
saveToken("https://app.prptl.org", "original-token");
|
|
370
|
+
const filePath = join(tempDir, "perpetual", "tokens.json");
|
|
371
|
+
const tmpPath = `${filePath}.tmp`;
|
|
372
|
+
const originalBytes = readFileSync(filePath, "utf-8");
|
|
373
|
+
|
|
374
|
+
vi.resetModules();
|
|
375
|
+
vi.doMock("node:fs", async () => {
|
|
376
|
+
const actual =
|
|
377
|
+
await vi.importActual<typeof import("node:fs")>("node:fs");
|
|
378
|
+
return {
|
|
379
|
+
...actual,
|
|
380
|
+
renameSync: () => {
|
|
381
|
+
throw new Error("simulated renameSync failure");
|
|
382
|
+
},
|
|
383
|
+
};
|
|
384
|
+
});
|
|
385
|
+
try {
|
|
386
|
+
const { saveToken: saveTokenFresh } = await import("../token-store.js");
|
|
387
|
+
expect(() =>
|
|
388
|
+
saveTokenFresh("https://app.prptl.org", "replacement-token"),
|
|
389
|
+
).toThrow(/Failed to write token file/);
|
|
390
|
+
} finally {
|
|
391
|
+
vi.doUnmock("node:fs");
|
|
392
|
+
vi.resetModules();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// The staged write succeeded, so .tmp existed at one point — the
|
|
396
|
+
// catch branch must have unlinked it.
|
|
397
|
+
expect(existsSync(tmpPath)).toBe(false);
|
|
398
|
+
// Original file is byte-for-byte unchanged.
|
|
399
|
+
expect(readFileSync(filePath, "utf-8")).toBe(originalBytes);
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
describe("clearToken", () => {
|
|
404
|
+
it("removes only the specified endpoint", () => {
|
|
405
|
+
saveToken("https://one.com", "token-one");
|
|
406
|
+
saveToken("https://two.com", "token-two");
|
|
407
|
+
|
|
408
|
+
clearToken("https://one.com");
|
|
409
|
+
|
|
410
|
+
expect(getStoredToken("https://one.com")).toBeNull();
|
|
411
|
+
expect(getStoredToken("https://two.com")?.token).toBe("token-two");
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("handles clearing a non-existent endpoint", () => {
|
|
415
|
+
saveToken("https://one.com", "token-one");
|
|
416
|
+
clearToken("https://nonexistent.com");
|
|
417
|
+
|
|
418
|
+
expect(getStoredToken("https://one.com")?.token).toBe("token-one");
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it("clears only the matching clientId for the same endpoint", () => {
|
|
422
|
+
saveToken("https://app.prptl.org", "token-a", { clientId: "client-a" });
|
|
423
|
+
saveToken("https://app.prptl.org", "token-b", { clientId: "client-b" });
|
|
424
|
+
|
|
425
|
+
clearToken("https://app.prptl.org", "client-a");
|
|
426
|
+
|
|
427
|
+
expect(getStoredToken("https://app.prptl.org", "client-a")).toBeNull();
|
|
428
|
+
expect(getStoredToken("https://app.prptl.org", "client-b")?.token).toBe(
|
|
429
|
+
"token-b",
|
|
430
|
+
);
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
describe("clearAllTokens", () => {
|
|
435
|
+
it("removes all tokens", () => {
|
|
436
|
+
saveToken("https://one.com", "token-one");
|
|
437
|
+
saveToken("https://two.com", "token-two");
|
|
438
|
+
|
|
439
|
+
clearAllTokens();
|
|
440
|
+
|
|
441
|
+
expect(getStoredToken("https://one.com")).toBeNull();
|
|
442
|
+
expect(getStoredToken("https://two.com")).toBeNull();
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("works when file does not exist yet", () => {
|
|
446
|
+
expect(() => clearAllTokens()).not.toThrow();
|
|
447
|
+
});
|
|
448
|
+
});
|
|
449
|
+
});
|