test.deploy/core/adapters/search.adapter.test.ts

test.deploy/core/adapters/search.adapter.test.ts is a file in Codebase Testing. 33 lines of code and 0 definitions.

import { afterEach, describe, expect, it, vi } from "vitest";
import { postPing, readLive } from "@banes-lab/deploy/core/adapters/search.adapter.ts";
import { requestOf } from "../reporters/webhook.fixture.ts";

const ENDPOINT = "https://ping.example.test/indexnow";
const PING = { host: "x.test", key: "abcd1234", keyLocation: "https://x.test/k.txt", urlList: ["https://x.test/"] };

const answer = function answer(status: number, body: string | null = null): ReturnType<typeof vi.fn<typeof fetch>> {
    const fetchMock = vi.fn<typeof fetch>().mockResolvedValue(new Response(body, { status }));
    vi.stubGlobal("fetch", fetchMock);
    return fetchMock;
};

describe("readLive", () => {
    afterEach(() => {
        vi.unstubAllGlobals();
    });

    it("reads the live body, and nothing for an address the site does not serve", async () => {
        answer(200, "<urlset/>");
        await expect(readLive("https://x.test/sitemap-pages.xml")).resolves.toBe("<urlset/>");
        answer(404, "missing");
        await expect(readLive("https://x.test/sitemap-pages.xml")).resolves.toBeNull();
    });
});

describe("postPing", () => {
    afterEach(() => {
        vi.unstubAllGlobals();
    });

    it("posts the ping as JSON and answers the endpoint's status", async () => {
        const fetchMock = answer(202);
        await expect(postPing(ENDPOINT, PING)).resolves.toBe(202);
        const request = requestOf(fetchMock);
        expect(request.method).toBe("POST");
        expect(JSON.parse(String(request.body))).toStrictEqual(PING);
    });
});