Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125import { describe, expect, it, vi } from "vitest";import { createBobbinClient } from "$lib/api/client";import { editSavedServices, hostOf, getPreferences } from "$lib/api/preferences";
const serving = (body: unknown) => vi.fn<typeof globalThis.fetch>( async () => new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } }) );
const contextFor = (body: unknown, fetch = serving(body)) => ({ ctx: createBobbinClient({ serviceUrl: "https://bobbin.oyster.cafe", fetch }), fetch});
describe("getPreferences", () => { it("splits knots from spindles, each in the order the account saved them", async () => { const { ctx, fetch } = contextFor({ preferences: [ { $type: "org.tangled.temp.actor.defs#savedSpindlePref", items: [{ id: "3l", value: "spindle.example" }] }, { $type: "org.tangled.temp.actor.defs#savedKnotPref", items: [ { id: "3a", value: "zebra.example" }, { id: "3b", value: "alpha.example" } ] } ] });
await expect(getPreferences(ctx)).resolves.toEqual({ knots: ["zebra.example", "alpha.example"], spindles: ["spindle.example"] }); expect(fetch.mock.calls.map(([input]) => new URL(String(input)).pathname)).toEqual([ "/xrpc/com.bad-example.pocket.getPreferences" ]); });
it("an account that saved nothing gets two empty lists", async () => { const { ctx } = contextFor({ preferences: [] });
await expect(getPreferences(ctx)).resolves.toEqual({ knots: [], spindles: [] }); });});
describe("hostOf", () => { it.each([ ["knot.example.com", "knot.example.com"], [" KNOT.Example.com ", "knot.example.com"], ["https://knot.example.com/", "knot.example.com"], ["http://knot.example.com:6000/some/path", "knot.example.com:6000"], ["", ""] ])("%s becomes %s", (input, expected) => { expect(hostOf(input)).toBe(expected); });});
describe("editSavedServices", () => { const roundTrip = () => { const sent: unknown[] = []; let stored = { preferences: [ { $type: "org.tangled.temp.actor.defs#savedKnotPref", items: [{ id: "one.example", value: "one.example" }] }, { $type: "org.tangled.temp.actor.defs#savedSpindlePref", items: [{ id: "spindle.example", value: "spindle.example" }] } ] }; const fetch = vi.fn<typeof globalThis.fetch>(async (input, init) => { if (String(input).endsWith("putPreferences")) { const body = JSON.parse(String(init?.body)) as typeof stored; sent.push(body); stored = body; return new Response(null, { status: 200 }); } return new Response(JSON.stringify(stored), { status: 200, headers: { "content-type": "application/json" } }); }); return { ctx: createBobbinClient({ serviceUrl: "https://bobbin.oyster.cafe", fetch }), sent, read: () => stored }; };
it("appending a knot leaves the spindles alone and reads back", async () => { const { ctx, sent } = roundTrip();
await expect( editSavedServices(ctx, "knots", (hosts) => [...hosts, "two.example"]) ).resolves.toEqual(["one.example", "two.example"]); expect(sent).toHaveLength(1); await expect(getPreferences(ctx)).resolves.toEqual({ knots: ["one.example", "two.example"], spindles: ["spindle.example"] }); });
it("a removed host is gone from the stored preferences", async () => { const { ctx } = roundTrip();
await editSavedServices(ctx, "spindles", (hosts) => hosts.filter((host) => host !== "spindle.example") );
await expect(getPreferences(ctx)).resolves.toEqual({ knots: ["one.example"], spindles: [] }); });});