Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2
3import { CursorKeychainTimeoutError, makeCachedCursorAccessTokenReader } from "./keychainToken.ts";
4
5describe("Cursor Keychain reader", () => {
6 it("shares concurrent reads and rechecks after the cache expires", async () => {
7 let reads = 0;
8 let time = 0;
9 const read = makeCachedCursorAccessTokenReader(
10 async () => {
11 reads++;
12 return `token-${reads}`;
13 },
14 () => time,
15 );
16 assert.deepStrictEqual(await Promise.all([read(), read()]), ["token-1", "token-1"]);
17 assert.strictEqual(await read(), "token-1");
18 assert.strictEqual(reads, 1);
19 time = 5 * 60_000;
20 assert.strictEqual(await read(), "token-2");
21 });
22
23 it("gives up on an unanswered prompt and reuses it on the next read", async () => {
24 let reads = 0;
25 let allow: (token: string) => void = () => {};
26 const read = makeCachedCursorAccessTokenReader(
27 () => {
28 reads++;
29 return new Promise((resolve) => {
30 allow = resolve;
31 });
32 },
33 () => 0,
34 1,
35 );
36 const error = await read().catch((cause: unknown) => cause);
37 assert.instanceOf(error, CursorKeychainTimeoutError);
38 const retry = read();
39 allow("token");
40 assert.strictEqual(await retry, "token");
41 assert.strictEqual(reads, 1);
42 });
43});