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