Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227import { describe, expect, it, vi } from "vitest";import { createBobbinClient } from "#lib/api/client.ts";import { completeOnboarding, dismissOnboardingNudge, editSavedServices, getOnboarding, getPreferences, hostOf, showsOnboardingNudge, startOnboarding} from "#lib/api/preferences.ts";
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.example.com", 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("a failed read throws instead of looking like an empty account", async () => { const fetch = vi.fn<typeof globalThis.fetch>( async () => new Response(JSON.stringify({ error: "InternalServerError" }), { status: 500 }) ); const { ctx } = contextFor(null, fetch);
await expect(getPreferences(ctx)).rejects.toThrow(); });
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.example.com", fetch }), sent, seed: (pref: Record<string, unknown>) => stored.preferences.push(pref as never), 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("leaves preference types it does not know about untouched", async () => { const { ctx, sent, seed } = roundTrip(); const foreign = { $type: "com.example.other#pref", flag: true }; seed(foreign);
await editSavedServices(ctx, "knots", (hosts) => [...hosts, "two.example"]);
expect((sent[0] as { preferences: unknown[] }).preferences).toContainEqual(foreign); });
it("never writes when the read fails", async () => { const fetch = vi.fn<typeof globalThis.fetch>( async () => new Response(null, { status: 503 }) ); const { ctx } = contextFor(null, fetch);
await expect(editSavedServices(ctx, "knots", (hosts) => hosts)).rejects.toThrow(); expect(fetch).toHaveBeenCalledTimes(1); });
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: [] }); });});
describe("onboarding state", () => { const pocket = (preferences: unknown[]) => { const sent: { preferences: unknown[] }[] = []; let stored = { preferences }; const fetch = vi.fn<typeof globalThis.fetch>(async (input, init) => { if (String(input).endsWith("putPreferences")) { stored = JSON.parse(String(init?.body)) as typeof stored; sent.push(stored); 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.example.com", fetch }), sent }; }; const knots = { $type: "org.tangled.temp.actor.defs#savedKnotPref", items: [{ id: "one.example", value: "one.example" }] };
it("treats an account that never started onboarding as done", async () => { const { ctx, sent } = pocket([knots]); await expect(getOnboarding(ctx)).resolves.toBeNull(); expect(showsOnboardingNudge(null)).toBe(false); await completeOnboarding(ctx); await dismissOnboardingNudge(ctx); expect(sent).toHaveLength(0); });
it("nudges a started account until it finishes", async () => { const { ctx } = pocket([knots]); await startOnboarding(ctx); expect(showsOnboardingNudge(await getOnboarding(ctx))).toBe(true); await completeOnboarding(ctx); const finished = await getOnboarding(ctx); expect(finished?.completedAt).toBeTypeOf("string"); expect(showsOnboardingNudge(finished)).toBe(false); });
it("stops nudging once the account asks not to be asked again", async () => { const { ctx } = pocket([knots]); await startOnboarding(ctx); await dismissOnboardingNudge(ctx); expect(showsOnboardingNudge(await getOnboarding(ctx))).toBe(false); });
it("keeps the first start time and the other preferences", async () => { const { ctx, sent } = pocket([knots]); await startOnboarding(ctx); const first = (await getOnboarding(ctx))?.startedAt; await startOnboarding(ctx); expect((await getOnboarding(ctx))?.startedAt).toBe(first); expect(sent).toHaveLength(1); expect(sent[0].preferences).toContainEqual(knots); });});