Suite of AT Protocol TypeScript libraries built on web standards
21
fork

Configure Feed

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

at main 92 lines 2.7 kB view raw
1import { assert, assertEquals, assertStrictEquals } from "@std/assert"; 2import type { DidString } from "@atp/lex"; 3import { type Agent, buildAgent, isAgent } from "../agent.ts"; 4 5Deno.test("buildAgent returns same object for Agent input", () => { 6 const agent: Agent = { 7 did: "did:plc:test" as DidString, 8 fetchHandler: (_path, _init) => Promise.resolve(new Response(null)), 9 }; 10 11 const result = buildAgent(agent); 12 assertStrictEquals(result, agent); 13}); 14 15Deno.test("buildAgent from service url constructs request url", async () => { 16 const calls: URL[] = []; 17 const fetchMock: typeof fetch = ((input: RequestInfo | URL) => { 18 const url = input instanceof URL ? input : new URL(String(input)); 19 calls.push(url); 20 return Promise.resolve(new Response(null)); 21 }) as typeof fetch; 22 23 const agent = buildAgent({ 24 service: "https://example.com", 25 fetch: fetchMock, 26 }); 27 28 await agent.fetchHandler("/xrpc/io.example.test?limit=1", { 29 method: "GET", 30 }); 31 32 assertEquals(calls.length, 1); 33 assertEquals( 34 calls[0]?.toString(), 35 "https://example.com/xrpc/io.example.test?limit=1", 36 ); 37}); 38 39Deno.test("buildAgent merges default and request headers with request precedence", async () => { 40 let seenHeaders: Headers | undefined; 41 const fetchMock: typeof fetch = ((_input, init) => { 42 seenHeaders = new Headers( 43 (init as { headers?: HeadersInit } | undefined)?.headers, 44 ); 45 return Promise.resolve(new Response(null)); 46 }) as typeof fetch; 47 48 const agent = buildAgent({ 49 service: "https://example.com", 50 headers: { 51 authorization: "Bearer default", 52 "x-default": "yes", 53 }, 54 fetch: fetchMock, 55 }); 56 57 await agent.fetchHandler("/xrpc/io.example.test", { 58 method: "GET", 59 headers: { 60 authorization: "Bearer request", 61 "x-request": "yes", 62 }, 63 }); 64 65 assert(seenHeaders != null); 66 assertEquals(seenHeaders.get("authorization"), "Bearer request"); 67 assertEquals(seenHeaders.get("x-default"), "yes"); 68 assertEquals(seenHeaders.get("x-request"), "yes"); 69}); 70 71Deno.test("buildAgent keeps did as live getter", () => { 72 const config: { did: DidString; service: string } = { 73 did: "did:plc:one" as DidString, 74 service: "https://example.com", 75 }; 76 const agent = buildAgent(config); 77 assertEquals(agent.did, "did:plc:one" as DidString); 78 config.did = "did:plc:two" as DidString; 79 assertEquals(agent.did, "did:plc:two" as DidString); 80}); 81 82Deno.test("isAgent detects valid and invalid values", () => { 83 assert(!isAgent(null)); 84 assert(!isAgent({})); 85 assert( 86 isAgent({ 87 did: "did:plc:test" as DidString, 88 fetchHandler: (_path: `/${string}`, _init: RequestInit) => 89 Promise.resolve(new Response(null)), 90 }), 91 ); 92});