@poncho-ai/harness 0.30.0 → 0.31.1

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.
@@ -0,0 +1,82 @@
1
+ import { mkdtemp } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ import { tmpdir } from "node:os";
4
+ import { afterEach, describe, expect, it, vi } from "vitest";
5
+ import {
6
+ deleteOpenAICodexSession,
7
+ getOpenAICodexAccessToken,
8
+ readOpenAICodexSession,
9
+ writeOpenAICodexSession,
10
+ } from "../src/openai-codex-auth.js";
11
+
12
+ const encodeJwt = (payload: Record<string, unknown>): string => {
13
+ const header = Buffer.from(JSON.stringify({ alg: "none", typ: "JWT" })).toString(
14
+ "base64url",
15
+ );
16
+ const body = Buffer.from(JSON.stringify(payload)).toString("base64url");
17
+ return `${header}.${body}.sig`;
18
+ };
19
+
20
+ describe("openai codex auth", () => {
21
+ afterEach(() => {
22
+ delete process.env.OPENAI_CODEX_AUTH_FILE;
23
+ vi.unstubAllGlobals();
24
+ vi.restoreAllMocks();
25
+ });
26
+
27
+ it("writes, reads, and deletes local session file", async () => {
28
+ const dir = await mkdtemp(join(tmpdir(), "poncho-codex-auth-"));
29
+ process.env.OPENAI_CODEX_AUTH_FILE = join(dir, "openai-codex.json");
30
+ await writeOpenAICodexSession({
31
+ refreshToken: "rt_123",
32
+ accessToken: "at_123",
33
+ accessTokenExpiresAt: Date.now() + 60_000,
34
+ accountId: "acct_abc",
35
+ });
36
+
37
+ const stored = await readOpenAICodexSession();
38
+ expect(stored?.refreshToken).toBe("rt_123");
39
+ expect(stored?.accessToken).toBe("at_123");
40
+ expect(stored?.accountId).toBe("acct_abc");
41
+
42
+ await deleteOpenAICodexSession();
43
+ const afterDelete = await readOpenAICodexSession();
44
+ expect(afterDelete).toBeUndefined();
45
+ });
46
+
47
+ it("refreshes token from stored session and persists rotated values", async () => {
48
+ const dir = await mkdtemp(join(tmpdir(), "poncho-codex-refresh-"));
49
+ process.env.OPENAI_CODEX_AUTH_FILE = join(dir, "openai-codex.json");
50
+ await writeOpenAICodexSession({
51
+ refreshToken: "rt_old",
52
+ accessToken: "at_expired",
53
+ accessTokenExpiresAt: Date.now() - 1000,
54
+ });
55
+
56
+ const idToken = encodeJwt({
57
+ "https://api.openai.com/auth": { chatgpt_account_id: "acct_rotated" },
58
+ });
59
+ const fetchMock = vi.fn(async () => {
60
+ return {
61
+ ok: true,
62
+ json: async () => ({
63
+ access_token: "at_new",
64
+ refresh_token: "rt_new",
65
+ expires_in: 3600,
66
+ id_token: idToken,
67
+ }),
68
+ } as Response;
69
+ });
70
+ vi.stubGlobal("fetch", fetchMock);
71
+
72
+ const access = await getOpenAICodexAccessToken();
73
+ expect(access.accessToken).toBe("at_new");
74
+ expect(access.accountId).toBe("acct_rotated");
75
+ expect(fetchMock).toHaveBeenCalledTimes(1);
76
+
77
+ const stored = await readOpenAICodexSession();
78
+ expect(stored?.refreshToken).toBe("rt_new");
79
+ expect(stored?.accessToken).toBe("at_new");
80
+ expect(stored?.accountId).toBe("acct_rotated");
81
+ });
82
+ });