import { describe, expect, it, vi } from "vitest"; import { createBobbinClient } from "#lib/api/client.ts"; import { fromTimeline, initialOf, loadLiveActivity } from "#lib/components/landing/hero/activity.ts"; import type { Fetch } from "#lib/fetch.ts"; const repo = (owner: string, slug: string) => ({ did: `did:plc:repo-${slug}`, slug, createdAt: "2026-10-15T07:00:00Z", owner: { did: `did:plc:${owner}`, handle: `${owner}.dev` } }); const actor = (name: string) => ({ did: `did:plc:${name}`, handle: `${name}.dev` }); const follow = (a: string, b: string) => ({ eventAt: "2026-10-15T10:00:00Z", event: { $type: "org.tangled.feed.getTimeline#followEvent" as const, cid: "c", uri: "at://x", actor: actor(a), subject: { ...actor(b), followersCount: 1, followsCount: 1 } } }); const star = (a: string, owner: string, slug: string) => ({ eventAt: "2026-10-15T10:00:00Z", event: { $type: "org.tangled.feed.getTimeline#starEvent" as const, cid: "c", uri: "at://x", actor: actor(a), repo: repo(owner, slug) } }); const created = (owner: string, slug: string, source?: ReturnType) => ({ eventAt: "2026-10-15T10:00:00Z", event: { $type: "org.tangled.feed.getTimeline#repoEvent" as const, cid: "c", uri: "at://x", repo: repo(owner, slug), source } }); // eslint-disable-next-line @typescript-eslint/no-explicit-any -- fixtures carry only what's read const feed = (...items: unknown[]) => items as any; describe("fromTimeline", () => { it("maps follows, stars and repos to notifications", () => { const out = fromTimeline( feed( follow("yukie", "oppi"), star("tobyn", "aly", "tg"), created("cris", "naut"), created("bobby", "naut-fork", repo("cris", "naut")) ) ); expect(out.map((n) => `${n.actor.name} ${n.verb} ${n.subject.name}`)).toEqual([ "yukie.dev followed oppi.dev", "tobyn.dev starred tg", "cris.dev created naut", "bobby.dev forked naut" ]); expect(out[0]).toMatchObject({ actor: { href: "/yukie.dev", avatar: expect.stringMatching(/^\/avatar\/did%3Aplc%3Ayukie/) }, subject: { href: "/oppi.dev", avatar: expect.stringMatching(/^\/avatar\/did%3Aplc%3Aoppi/) } }); expect(out[1].subject).toMatchObject({ href: "/aly.dev/tg" }); expect(out[1].subject.avatar).toMatch(/did%3Aplc%3Aaly/); expect(out[2].subject.avatar).toBeUndefined(); // the letter shown if a face won't load: the person's, or the starred repo's owner's expect(out[0].actor.initial).toBe("Y"); expect(out[1].subject.initial).toBe("A"); }); it("keeps when each event happened", () => { const late = { ...follow("a", "b"), eventAt: "2026-10-15T11:30:00Z" }; expect(fromTimeline(feed(late, star("c", "d", "r"))).map((n) => n.at)).toEqual([ "2026-10-15T11:30:00Z", "2026-10-15T10:00:00Z" ]); }); it("takes the first letter or digit of a handle as its initial", () => { expect(initialOf("oppi.lo")).toBe("O"); expect(initialOf("_3d.dev")).toBe("3"); expect(initialOf("")).toBe("?"); }); it("drops events with an unresolved handle", () => { const broken = follow("a", "b"); broken.event.actor.handle = "handle.invalid"; expect(fromTimeline(feed(broken))).toEqual([]); }); it("skips an event missing a field its type promises, keeping the rest", () => { const ownerless = star("a", "c", "r"); // @ts-expect-error -- the appview ships apart from this app; the shape isn't guaranteed delete ownerless.event.repo.owner; const out = fromTimeline(feed(ownerless, follow("b", "c"))); expect(out.map((n) => n.actor.name)).toEqual(["b.dev"]); }); }); describe("loadLiveActivity", () => { const items = Array.from({ length: 8 }, (_, i) => follow(`u${i}`, "oppi")); const json = (body: unknown, ok = true) => Promise.resolve( new Response(JSON.stringify(body), { status: ok ? 200 : 500, headers: { "content-type": "application/json" } }) ); const ctxWith = (fetch: (url: string | URL) => Promise) => createBobbinClient({ serviceUrl: "https://bobbin.test", fetch: fetch as unknown as Fetch }); it("reads only the bobbin's timeline", async () => { const fetch = vi.fn((_url: string | URL) => json({ feed: items })); const out = await loadLiveActivity(ctxWith(fetch)); expect(out).toHaveLength(8); expect(fetch.mock.calls.map(([u]) => new URL(String(u)).host)).toEqual(["bobbin.test"]); }); it("is null when no timeline answers", async () => { const fetch = vi.fn(() => json({}, false)); expect(await loadLiveActivity(ctxWith(fetch))).toBeNull(); }); });