evOo-local/apps/www/src/github.test.ts
cynarAI 6c7eed7580 Initial clean evOo-local from f00577b
Fresh history cut from cynarAI/evoo antigravity-handoff/project-timeline
(Voice First + project timeline). Brand docs/titles as evOo; keep @rakazo/*
packages as tech debt. Excludes node_modules, caches, secrets, voice blobs.
2026-09-20 17:15:16 +02:00

48 lines
1.6 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import {
fetchGithubStars,
formatStarCount,
GITHUB_STARS_TIMEOUT_MS,
MAX_GITHUB_STARS_RESPONSE_BYTES,
} from "./github.js";
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
describe("GitHub stars", () => {
it("returns a numeric count and formats compact labels", async () => {
const fetchImpl = vi.fn(async () => Response.json({ stargazers_count: 12_345 }));
await expect(fetchGithubStars(fetchImpl as unknown as typeof fetch)).resolves.toBe(12_345);
expect(formatStarCount(999)).toBe("999");
expect(formatStarCount(1_250)).toBe("1.3k");
expect(formatStarCount(12_345)).toBe("12k");
});
it("returns the fallback when GitHub does not settle before the deadline", async () => {
vi.useFakeTimers();
const fetchImpl = vi.fn((_input: RequestInfo | URL, _init?: RequestInit) =>
new Promise<Response>(() => undefined),
);
const pending = fetchGithubStars(fetchImpl as typeof fetch);
await vi.advanceTimersByTimeAsync(GITHUB_STARS_TIMEOUT_MS);
await expect(pending).resolves.toBeNull();
expect(fetchImpl.mock.calls[0]?.[1]?.signal?.aborted).toBe(true);
});
it("returns the fallback for an oversized GitHub response", async () => {
const fetchImpl = vi.fn(async () =>
Response.json(
{ stargazers_count: 12_345 },
{ headers: { "content-length": String(MAX_GITHUB_STARS_RESPONSE_BYTES + 1) } },
),
);
await expect(fetchGithubStars(fetchImpl as unknown as typeof fetch)).resolves.toBeNull();
});
});