aboutsummaryrefslogtreecommitdiff
path: root/tests/apq.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tests/apq.test.ts')
-rw-r--r--tests/apq.test.ts70
1 files changed, 35 insertions, 35 deletions
diff --git a/tests/apq.test.ts b/tests/apq.test.ts
index 66607cd..2097377 100644
--- a/tests/apq.test.ts
+++ b/tests/apq.test.ts
@@ -10,43 +10,9 @@ import { expect, test } from "vitest";
import { createApqFetcher } from "../source/library/fetcher/apq.ts";
-/*** HELPERS ------------------------------------------ ***/
-
type Call = { body: Record<string, unknown>; url: string };
-function createStub(queue: Array<Record<string, unknown>>): {
- calls: Call[];
- stub: typeof fetch;
-} {
- const calls: Call[] = [];
-
- const stub: typeof fetch = (input, init) => {
- const body = JSON.parse((init?.body as string) ?? "null") as Record<string, unknown>;
- calls.push({ body, url: String(input) });
-
- const next = queue.shift();
-
- if (next === undefined)
- throw new Error("stub fetch exhausted");
-
- return Promise.resolve(new Response(JSON.stringify(next), { status: 200 }));
- };
-
- return { calls, stub };
-}
-
-async function expectedHash(query: string): Promise<string> {
- const buf = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(query));
- return Array.from(new Uint8Array(buf)).map((b) => b.toString(16).padStart(2, "0")).join("");
-}
-
-function readPersistedHash(body: Record<string, unknown>): string {
- const extensions = body.extensions as Record<string, unknown>;
- const persistedQuery = extensions.persistedQuery as Record<string, unknown>;
- return persistedQuery.sha256Hash as string;
-}
-
-/*** TESTS -------------------------------------------- ***/
+/*** PROGRAM ------------------------------------------ ***/
test("apq cache miss retries with the full query", async () => {
const { calls, stub } = createStub([
@@ -149,3 +115,37 @@ test("apq accepts PERSISTED_QUERY_NOT_FOUND extension code", async () => {
expect(calls.length).toEqual(2);
expect(calls[1].body.query).toEqual("{ hello }");
});
+
+/*** HELPER ------------------------------------------- ***/
+
+function createStub(queue: Array<Record<string, unknown>>): {
+ calls: Call[];
+ stub: typeof fetch;
+} {
+ const calls: Call[] = [];
+
+ const stub: typeof fetch = (input, init) => {
+ const body = JSON.parse((init?.body as string) ?? "null") as Record<string, unknown>;
+ calls.push({ body, url: String(input) });
+
+ const next = queue.shift();
+
+ if (next === undefined)
+ throw new Error("stub fetch exhausted");
+
+ return Promise.resolve(new Response(JSON.stringify(next), { status: 200 }));
+ };
+
+ return { calls, stub };
+}
+
+async function expectedHash(query: string): Promise<string> {
+ const buf = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(query));
+ return Array.from(new Uint8Array(buf)).map((b) => b.toString(16).padStart(2, "0")).join("");
+}
+
+function readPersistedHash(body: Record<string, unknown>): string {
+ const extensions = body.extensions as Record<string, unknown>;
+ const persistedQuery = extensions.persistedQuery as Record<string, unknown>;
+ return persistedQuery.sha256Hash as string;
+}