Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Runs the test suite on every branch push and pull request. Publishing keeps
# its own release-triggered workflow in npm-publish.yml.
name: CI

on: [push, pull_request]

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
# The tests stub the Pi SDK, so no dependencies are installed.
- run: npm test
21 changes: 18 additions & 3 deletions test/claude-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,23 @@ registerHooks({ resolve(specifier, context, next) {
? { url: sdkUrl, shortCircuit: true } : next(specifier, context);
} });

/**
* The extension settles windows whose reset has passed, so the running-clock test
* needs resets ahead of now; the fixed timestamps above are for pure normalization.
*/
function bridgeHeadersFromNow() {
const nowSeconds = Math.floor(Date.now() / 1_000);
return {
...bridgeHeaders,
"anthropic-ratelimit-unified-reset": String(nowSeconds + 7_200),
"anthropic-ratelimit-unified-5h-reset": String(nowSeconds + 7_200),
"anthropic-ratelimit-unified-7d-reset": String(nowSeconds + 259_200),
};
}

test("extension shows observed Claude usage without any request", async () => {
const { default: extension } = await import("../src/usage.ts");
const liveHeaders = bridgeHeadersFromNow();
const handlers = new Map<string, Function>();
const commands = new Map<string, { handler: Function }>();
extension({
Expand Down Expand Up @@ -182,18 +197,18 @@ test("extension shows observed Claude usage without any request", async () => {
assert.match(await usage() ?? "", /Waiting: /);
assert.equal(statuses.at(-1), undefined);

await emit("after_provider_response", { status: 200, headers: { ...bridgeHeaders, "content-type": "text/event-stream" } });
await emit("after_provider_response", { status: 200, headers: { ...liveHeaders, "content-type": "text/event-stream" } });
assert.match(await usage() ?? "", /Waiting: /);

await emit("after_provider_response", { status: 200, headers: bridgeHeaders });
await emit("after_provider_response", { status: 200, headers: liveHeaders });
assert.match(statuses.at(-1) ?? "", /^5h 80% .* 1w 87% /);
assert.ok(!statuses.includes("usage …"));

// A bridge report arriving after the switch away must not overwrite the observation.
ctx.model = { provider: "unknown-provider", id: "other" };
await emit("model_select", { model: ctx.model });
assert.equal(statuses.at(-1), undefined);
await emit("after_provider_response", { status: 200, headers: { ...bridgeHeaders, "anthropic-ratelimit-unified-5h-utilization": "0.9" } });
await emit("after_provider_response", { status: 200, headers: { ...liveHeaders, "anthropic-ratelimit-unified-5h-utilization": "0.9" } });
ctx.model = model;
await emit("model_select", { model: ctx.model });
assert.match(statuses.at(-1) ?? "", /^5h 80% /);
Expand Down