test: add fetch mock helper and reaction coverage

This commit is contained in:
Sebastian
2026-02-17 09:01:30 -05:00
parent 0e023e300e
commit cc359d338e
28 changed files with 193 additions and 106 deletions

View File

@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
import { withFetchPreconnect } from "../test-utils/fetch-mock.js";
import {
CHUTES_TOKEN_ENDPOINT,
CHUTES_USERINFO_ENDPOINT,
@@ -15,7 +16,7 @@ const urlToString = (url: Request | URL | string): string => {
describe("chutes-oauth", () => {
it("exchanges code for tokens and stores username as email", async () => {
const fetchFn: typeof fetch = async (input, init) => {
const fetchFn = withFetchPreconnect(async (input, init) => {
const url = urlToString(input);
if (url === CHUTES_TOKEN_ENDPOINT) {
expect(init?.method).toBe("POST");
@@ -41,7 +42,7 @@ describe("chutes-oauth", () => {
});
}
return new Response("not found", { status: 404 });
};
});
const now = 1_000_000;
const creds = await exchangeChutesCodeForTokens({
@@ -65,7 +66,7 @@ describe("chutes-oauth", () => {
});
it("refreshes tokens using stored client id and falls back to old refresh token", async () => {
const fetchFn: typeof fetch = async (input, init) => {
const fetchFn = withFetchPreconnect(async (input, init) => {
const url = urlToString(input);
if (url !== CHUTES_TOKEN_ENDPOINT) {
return new Response("not found", { status: 404 });
@@ -82,7 +83,7 @@ describe("chutes-oauth", () => {
}),
{ status: 200, headers: { "Content-Type": "application/json" } },
);
};
});
const now = 2_000_000;
const refreshed = await refreshChutesTokens({