This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / web / src / lib / api / knotmirror.test.ts
2.1 kB 67 lines
1import { describe, expect, it, vi } from "vitest"; 2import * as knotmirror from "./knotmirror"; 3 4const jsonResponse = (body: unknown): Response => 5 new Response(JSON.stringify(body), { 6 status: 200, 7 headers: { "content-type": "application/json" } 8 }); 9 10describe("knotmirror.tree", () => { 11 it("adapts the temporary tree response and fetches the readme blob", async () => { 12 const fetchMock = vi.fn<typeof globalThis.fetch>().mockImplementation(async (input) => { 13 const url = new URL(String(input)); 14 if (url.pathname.endsWith("getTree")) { 15 return jsonResponse({ 16 ref: "main", 17 files: [ 18 { 19 name: "README.md", 20 mode: "100644", 21 size: 12, 22 last_commit: { 23 hash: "abc", 24 message: "docs", 25 when: "2026-07-01T00:00:00Z" 26 } 27 } 28 ] 29 }); 30 } 31 return new Response("# hello", { 32 status: 200, 33 headers: { "content-type": "text/plain" } 34 }); 35 }); 36 37 const ctx = knotmirror.createKnotMirrorClient("https://mirror.test", fetchMock); 38 const tree = await knotmirror.tree(ctx, { 39 repo: "did:plc:repo", 40 ref: "main", 41 path: "docs" 42 }); 43 44 expect(tree.readme).toEqual({ filename: "README.md", contents: "# hello" }); 45 expect(tree.files[0]).toMatchObject({ name: "README.md", mode: "100644" }); 46 expect(fetchMock).toHaveBeenCalledTimes(2); 47 const blobUrl = new URL(String(fetchMock.mock.calls[1][0])); 48 expect(blobUrl.pathname).toBe("/xrpc/sh.tangled.git.temp.getBlob"); 49 expect(blobUrl.searchParams.get("path")).toBe("docs/README.md"); 50 }); 51}); 52 53describe("knotmirror.languages", () => { 54 it("uses the temporary languages endpoint", async () => { 55 const fetchMock = vi 56 .fn<typeof globalThis.fetch>() 57 .mockResolvedValue( 58 jsonResponse({ ref: "main", languages: [{ name: "TypeScript", size: 10 }] }) 59 ); 60 const ctx = knotmirror.createKnotMirrorClient("https://mirror.test", fetchMock); 61 62 await knotmirror.languages(ctx, { repo: "did:plc:repo", ref: "main" }); 63 64 const url = new URL(String(fetchMock.mock.calls[0][0])); 65 expect(url.pathname).toBe("/xrpc/sh.tangled.git.temp.listLanguages"); 66 }); 67});