Files
paseo/plugins/claude-usage-source/server/keychain.test.ts
T
Mohamed Boudra 52377f595a Enable sidebar usage summary by default and move source toggles to a dedicated Sources list (#10491)
* feat(usage): enable sidebar usage summary by default and add dedicated Sources list

Show the sidebar Usage summary by default instead of requiring opt-in in
Appearance settings, while honoring explicit off preferences and continuing
to hide the row when no enabled source has pinned windows.

Let usage sources declare optional provider hints (`providers` on the
registration and `provider` on discovered accounts) so the daemon can mark
reports `defaultEnabled: false` when their hinted providers are neither
enabled nor installed, keeping usage sources decoupled from agent providers
while avoiding clutter from leftover credentials.

Move per-source show/hide controls out of individual card headers into a
dedicated `Sources` submenu inside Usage Settings (with tri-state
`cardVisibility` preferences migrating legacy `cardsOff`) so disabled
sources stay out of the main card list.

* fix(usage): resolve provider hints per report, refresh on forceRefresh, and clarify inactive copy

Resolve each usage report's provider activity hint in parallel with its
fetch and enforce a real timeout deadline in `isProviderActiveForUsage` so
a slow provider probe never holds back unrelated reports or hangs past the
deadline on non-abortable plugin RPCs.

Give the availability hint a 5-minute freshness TTL and re-probe when
`forceRefresh` is requested, retaining the fresher probe result across
subsequent normal reads until a newer snapshot or TTL expiry supersedes it.

Rename the default-off Sources sublabel from "Provider not installed" to
"Provider inactive" across all locales so it accurately covers both
disabled and unavailable providers.

* fix(usage): share provider availability reading across snapshot publishes and usage probes

Record both `ready` and `unavailable` global snapshot publications into
`usageActivityCache` so `isProviderActiveForUsage` reads one timestamped
availability cache instead of relying on `fetchedAt` (which unavailable
snapshot entries do not carry).

* fix(app): add MenuCheckboxItem so Usage Sources toggles expose checked state to screen readers

Introduce `MenuCheckboxItem` / `DropdownMenuCheckboxItem` backed by
`accessibilityRole="checkbox"` so toggleable menu rows expose their checked
state in Chromium's accessibility tree and native screen readers while
leaving existing `MenuItem` roles unchanged.

* fix(app): keep compact menu sheet rows in Tab order and activate on Space

Use `tabIndex={0}` for `MenuRow` in `sheet` presentation (leaving
popover roving arrow-key navigation at `tabIndex={-1}`) and handle Space
in web sheet rows since React Native Web only activates Space for
`role="button"`, not `menuitem` or `checkbox`.

* test(usage): include provider hint in claude-usage-source keychain and stores assertions
2026-11-22 16:43:11 +07:00

197 lines
6.8 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { claudeKeychainAccount, readClaudeKeychainCredentials } from "./usage.js";
const SERVICE = "Claude Code-credentials";
afterEach(() => {
vi.unstubAllEnvs();
});
describe("claudeKeychainAccount", () => {
it("uses a supported username verbatim", () => {
expect(claudeKeychainAccount("thomas.benoit")).toBe("thomas.benoit");
});
it("falls back to claude-code-user when the username carries a rejected character", () => {
expect(claudeKeychainAccount("first.last@example.com")).toBe("claude-code-user");
});
it("reads the username from $USER when no account is given", () => {
vi.stubEnv("USER", "ci-runner");
expect(claudeKeychainAccount()).toBe("ci-runner");
vi.stubEnv("USER", "first.last@example.com");
expect(claudeKeychainAccount()).toBe("claude-code-user");
});
});
describe("readClaudeKeychainCredentials", () => {
it("looks the item up by account and stops there when it exists", async () => {
const run = vi.fn(async () => JSON.stringify({ claudeAiOauth: { accessToken: "at_fresh" } }));
const credentials = await readClaudeKeychainCredentials(run, "claude-code-user");
expect(credentials).toEqual({ claudeAiOauth: { accessToken: "at_fresh" } });
expect(run).toHaveBeenCalledTimes(1);
expect(run).toHaveBeenCalledWith([
"find-generic-password",
"-a",
"claude-code-user",
"-w",
"-s",
SERVICE,
]);
});
it("falls back to the account-less lookup when no item matches the account", async () => {
const run = vi.fn(async (args: string[]) =>
args.includes("-a") ? null : JSON.stringify({ claudeAiOauth: { accessToken: "at_legacy" } }),
);
const credentials = await readClaudeKeychainCredentials(run, "claude-code-user");
expect(credentials).toEqual({ claudeAiOauth: { accessToken: "at_legacy" } });
expect(run).toHaveBeenNthCalledWith(2, ["find-generic-password", "-w", "-s", SERVICE]);
});
it("falls through when the account item carries no access token", async () => {
const run = vi.fn(async (args: string[]) =>
args.includes("-a")
? JSON.stringify({ claudeAiOauth: { subscriptionType: "team" } })
: JSON.stringify({ claudeAiOauth: { accessToken: "at_legacy" } }),
);
const credentials = await readClaudeKeychainCredentials(run, "claude-code-user");
expect(credentials).toEqual({ claudeAiOauth: { accessToken: "at_legacy" } });
expect(run).toHaveBeenCalledTimes(2);
});
it("is null when the Keychain holds no item at all", async () => {
const run = vi.fn(async () => null);
expect(await readClaudeKeychainCredentials(run, "claude-code-user")).toBeNull();
});
it("tries the next lookup when the item does not parse", async () => {
const run = vi.fn(async (args: string[]) =>
args.includes("-a")
? "not-json"
: JSON.stringify({ claudeAiOauth: { accessToken: "at_legacy" } }),
);
expect(await readClaudeKeychainCredentials(run, "claude-code-user")).toEqual({
claudeAiOauth: { accessToken: "at_legacy" },
});
});
});
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { discover, fetchUsage } from "./usage.js";
import { inputSchema } from "../shared/input.js";
function fixtureFetch(expectedToken: string): typeof fetch {
return vi.fn(async (_url: RequestInfo | URL, init?: RequestInit) => {
expect(init?.headers).toMatchObject({ Authorization: `Bearer ${expectedToken}` });
return new Response(
JSON.stringify({
account: { uuid: "fixture-account" },
organization: { uuid: "fixture-org" },
five_hour: { utilization: 12 },
}),
{ status: 200 },
);
}) as unknown as typeof fetch;
}
describe("Claude credential routes", () => {
it("reads the explicit Claude credential file", async () => {
const dir = mkdtempSync(join(tmpdir(), "claude-usage-input-"));
try {
writeFileSync(
join(dir, ".credentials.json"),
JSON.stringify({ claudeAiOauth: { accessToken: "fixture-file" } }),
);
expect(
(
await fetchUsage(
{ route: { store: "claude", path: join(dir, ".credentials.json") } },
fixtureFetch("fixture-file"),
)
).status,
).toBe("available");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("discovery reads the configured credential file", async () => {
const dir = mkdtempSync(join(tmpdir(), "claude-usage-default-"));
try {
writeFileSync(
join(dir, ".credentials.json"),
JSON.stringify({ claudeAiOauth: { accessToken: "fixture-default" } }),
);
const accounts = await discover(
{ kind: "global" },
{ home: dir, env: { CLAUDE_CONFIG_DIR: dir }, platform: "linux" },
fixtureFetch("fixture-default"),
);
expect(accounts).toEqual([
{
key: "fixture-account.fixture-org",
harness: "Claude",
provider: "claude",
input: {
route: { store: "claude", path: join(dir, ".credentials.json") },
accountKey: "fixture-account.fixture-org",
},
},
]);
expect(
(await fetchUsage(inputSchema.parse(accounts[0]!.input), fixtureFetch("fixture-default")))
.status,
).toBe("available");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("throws without fetching when the routed file has no credentials", async () => {
const dir = mkdtempSync(join(tmpdir(), "claude-empty-config-"));
try {
const fetchApi = vi.fn() as unknown as typeof fetch;
await expect(
fetchUsage({ route: { store: "claude", path: join(dir, ".credentials.json") } }, fetchApi, {
platform: "linux",
}),
).rejects.toThrow("Claude login store no longer exists");
expect(fetchApi).not.toHaveBeenCalled();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe("Claude Keychain token usage", () => {
it("returns unavailable on 401 without attempting refresh", async () => {
const run = vi.fn(async () =>
JSON.stringify({
claudeAiOauth: { accessToken: "fixture-expired", refreshToken: "fixture-refresh" },
}),
);
const credentials = await readClaudeKeychainCredentials(run, "fixture-account");
const fetchApi = vi.fn(
async () => new Response(null, { status: 401 }),
) as unknown as typeof fetch;
const report = await fetchUsage({ route: { store: "keychain" } }, fetchApi, {
platform: "darwin",
claudeHome: "/nonexistent-paseo-test-home",
readKeychainCredentials: async () => credentials,
});
expect(report.status).toBe("unavailable");
expect(fetchApi).toHaveBeenCalledTimes(1);
expect(run).toHaveBeenCalledTimes(1);
});
});