import { describe, expect, test } from "bun:test" import { ServerConnection } from "@/runtime/server/registry" import { DEFAULT_NAVIGATION, EMPTY_FILTERS, EMPTY_NAVIGATION_GROUPS, draftKey, navigationKey, type NavDraft, type NavigationSettings, type NavSession, type Section, type ViewInput, } from "./contract" import { familyRequests, moreStep, navigationView } from "./view" const NOW = 1_800_000_000_000 const minute = 60_000 const hour = 60 * minute const day = 24 * hour const LOCAL = ServerConnection.Key.make("local") const REMOTE = ServerConnection.Key.make("remote") const key = (id: string, server = LOCAL) => navigationKey(server, id) const session = (id: string, patch: Partial = {}): NavSession => ({ key: key(id, patch.server ?? LOCAL), id, server: LOCAL, title: id, projectID: "app", directory: "/repo/app", worktree: undefined, parentID: undefined, created: NOW - day, activity: NOW - hour, archived: false, status: "idle", muted: false, ...patch, }) const draft = (id: string, patch: Partial = {}): NavDraft => ({ key: draftKey(id), draftID: id, server: LOCAL, projectID: "app", directory: "/repo/app", preview: `draft ${id}`, updated: NOW - minute, ...patch, }) // Twenty "app" sessions a minute apart, eight "web" sessions on the remote server, and one with no project. const SESSIONS = [ ...Array.from({ length: 20 }, (_, index) => session(`app-${index}`, { activity: NOW - (index + 1) * minute })), ...Array.from({ length: 8 }, (_, index) => session(`web-${index}`, { server: REMOTE, projectID: "web", activity: NOW - (index + 1) * 2 * minute }), ), session("loose", { projectID: undefined, activity: NOW - 10 * day }), ] const input = (patch: Partial = {}): ViewInput => ({ sessions: SESSIONS, drafts: [], pins: [], groups: EMPTY_NAVIGATION_GROUPS, selected: undefined, open: [], now: NOW, servers: [ { key: LOCAL, name: "Local" }, { key: REMOTE, name: "Remote" }, ], projects: [ { server: LOCAL, id: "app", name: "App" }, { server: REMOTE, id: "web", name: "Website" }, ], view: { filters: EMPTY_FILTERS, collapsed: {}, expanded: {} }, ...patch, }) const view = (patch: Partial = {}, settings: Partial = {}) => navigationView(input(patch), { ...DEFAULT_NAVIGATION, ...settings }) const section = (sections: Section[], id: string) => sections.find((item) => item.id === id) const keys = (item: Section | undefined) => item?.rows.map((row) => row.key) const replace = (...patches: NavSession[]) => { const map = new Map(SESSIONS.map((item) => [item.key, item])) patches.forEach((item) => map.set(item.key, item)) return [...map.values()] } describe("navigationView", () => { test("Don't group is one headerless list that Show limits, newest activity first", () => { const result = view({}, { groupBy: "none", show: "10", rowsPerGroup: "3" }) const main = section(result.sections, "main") expect(result.sections.filter((item) => item.kind === "group")).toEqual([]) expect(main?.heading).toBeUndefined() expect(main?.rows).toHaveLength(10) expect(main?.more).toBe(SESSIONS.length - 10) expect(keys(main)?.slice(0, 3)).toEqual([key("app-0"), key("app-1"), key("web-0", REMOTE)]) expect(result.order).toEqual(keys(main) ?? []) }) test("Show last 7 days hides older sessions, and Show more reveals them", () => { const week = view({}, { show: "week" }) expect(keys(section(week.sections, "main"))).not.toContain(key("loose")) expect(section(week.sections, "main")?.more).toBe(1) const expanded = view({ view: { filters: EMPTY_FILTERS, collapsed: {}, expanded: { main: 10 } } }, { show: "week" }) expect(keys(section(expanded.sections, "main"))).toContain(key("loose")) }) test("Rows per group limits every headed group, and Show doesn't", () => { const three = view({}, { groupBy: "project", rowsPerGroup: "3", show: "all" }).sections const all = view({}, { groupBy: "project", rowsPerGroup: "all", show: "10" }).sections expect(three.map((item) => item.id)).toEqual([ `project:${key("app")}`, `project:${key("web", REMOTE)}`, "project:none", ]) three.forEach((item) => { expect(item.rows.length).toBeLessThanOrEqual(3) expect(item.more).toBe(item.total - item.rows.length) }) expect(three[0]?.heading).toEqual({ type: "project", server: LOCAL, projectID: "app", name: "App" }) all.forEach((item) => expect(item.more).toBe(0)) }) test("Rows per group defaults to 5", () => { const app = section(view({}, { groupBy: "project" }).sections, `project:${key("app")}`) expect(DEFAULT_NAVIGATION.rowsPerGroup).toBe("5") expect(app?.rows).toHaveLength(5) }) test("sessions that need you and the selected one show past the limit", () => { const sessions = replace( session("app-15", { status: "question", requestSince: NOW - minute, activity: NOW - 16 * minute }), ) const result = view({ sessions, selected: key("app-18") }, { groupBy: "project", rowsPerGroup: "3" }) const app = section(result.sections, `project:${key("app")}`) expect(keys(app)).toEqual([key("app-0"), key("app-1"), key("app-2"), key("app-15"), key("app-18")]) expect(app?.more).toBe(15) }) test("Show more adds Rows per group to a group, and Show's step to the ungrouped list", () => { const result = view( { view: { filters: EMPTY_FILTERS, collapsed: {}, expanded: { [`project:${key("app")}`]: 3 } } }, { groupBy: "project", rowsPerGroup: "3" }, ) expect(section(result.sections, `project:${key("app")}`)?.rows).toHaveLength(6) expect(moreStep("group", { show: "30", rowsPerGroup: "3" })).toBe(3) expect(moreStep("main", { show: "30", rowsPerGroup: "3" })).toBe(30) expect(moreStep("main", { show: "week", rowsPerGroup: "all" })).toBe(10) }) test("drafts come first, then Pinned above every group whatever the server", () => { const result = view( { pins: [key("web-3", REMOTE), key("app-4")], drafts: [draft("old", { updated: NOW - hour }), draft("new")] }, { groupBy: "server" }, ) expect(result.sections.map((item) => item.id)).toEqual(["drafts", "pinned", `server:${LOCAL}`, `server:${REMOTE}`]) expect(section(result.sections, "drafts")?.heading).toBeUndefined() expect(keys(section(result.sections, "drafts"))).toEqual([draftKey("new"), draftKey("old")]) expect(keys(section(result.sections, "pinned"))).toEqual([key("web-3", REMOTE), key("app-4")]) expect(keys(section(result.sections, `server:${LOCAL}`))).not.toContain(key("app-4")) expect(section(result.sections, `server:${REMOTE}`)?.heading).toEqual({ type: "server", server: REMOTE, name: "Remote", }) }) test("Group by Status: needs you by longest wait, running by start, then recent", () => { const sessions = replace( session("app-5", { status: "approval", requestSince: NOW - 2 * minute }), session("app-9", { status: "question", requestSince: NOW - 10 * minute }), session("app-1", { status: "working", runSince: NOW - minute }), session("app-7", { status: "working", runSince: NOW - 30 * minute }), ) const result = view({ sessions }, { groupBy: "status", rowsPerGroup: "all" }) expect(result.sections.map((item) => item.heading)).toEqual([ { type: "status", status: "attention" }, { type: "status", status: "running" }, { type: "status", status: "recent" }, ]) expect(keys(result.sections[0])).toEqual([key("app-9"), key("app-5")]) expect(keys(result.sections[1])).toEqual([key("app-7"), key("app-1")]) expect(result.sections[0]?.rows[0]).toMatchObject({ status: "question", request: { kind: "question" } }) }) test("Running at bottom moves running sessions to a shelf after the list", () => { const sessions = replace(session("app-0", { status: "working", runSince: NOW - minute })) const result = view({ sessions }, { runningAtBottom: true, show: "all" }) expect(result.sections.map((item) => item.id)).toEqual(["main", "working"]) expect(keys(section(result.sections, "working"))).toEqual([key("app-0")]) expect(keys(section(result.sections, "main"))).not.toContain(key("app-0")) expect(section(result.sections, "working")?.heading).toEqual({ type: "running" }) }) test("Group by Manual shows empty groups and keeps the manual order", () => { const groups = { groups: [ { id: "release", name: "Release", color: "blue" as const }, { id: "empty", name: "Empty", color: "green" as const }, ], membership: { [key("app-3")]: "release", [key("app-1")]: "release", [key("app-2")]: "release" }, order: [key("app-3"), key("app-1")], } const result = view({ groups }, { groupBy: "manual", rowsPerGroup: "all" }) expect(result.sections.map((item) => item.id).slice(0, 3)).toEqual(["group:release", "group:empty", "main"]) // Sessions missing from the order go first, then yours. expect(keys(section(result.sections, "group:release"))).toEqual([key("app-2"), key("app-3"), key("app-1")]) expect(section(result.sections, "group:empty")?.rows).toEqual([]) expect(section(result.sections, "group:release")?.rows[0]).toMatchObject({ group: { id: "release" } }) expect(keys(section(result.sections, "main"))).not.toContain(key("app-3")) }) test("with Subagents off, children are hidden and the root carries the oldest request below it", () => { const sessions = [ ...SESSIONS, session("child", { parentID: "app-0", status: "approval", requestSince: NOW - 5 * minute }), session("grandchild", { parentID: "child", status: "question", requestSince: NOW - 9 * minute }), ] const result = view({ sessions }, { subagents: false, show: "all" }) const root = section(result.sections, "main")?.rows.find((row) => row.key === key("app-0")) expect(result.order).not.toContain(key("child")) expect(result.order).not.toContain(key("grandchild")) expect(root).toMatchObject({ status: "question", request: { key: key("grandchild"), kind: "question" } }) }) test("with Subagents on, children nest under their parent with their own requests", () => { const sessions = [ ...SESSIONS, session("child", { parentID: "app-0", status: "approval", requestSince: NOW - 5 * minute, activity: NOW }), session("grandchild", { parentID: "child", activity: NOW - 20 * day }), ] const result = view({ sessions }, { subagents: true, show: "10" }) const main = section(result.sections, "main") expect(keys(main)?.slice(0, 3)).toEqual([key("app-0"), key("child"), key("grandchild")]) expect(main?.rows.slice(0, 3).map((row) => (row.kind === "session" ? row.depth : -1))).toEqual([0, 1, 2]) expect(main?.rows[0]).toMatchObject({ status: "idle", request: undefined }) expect(main?.rows[1]).toMatchObject({ status: "approval" }) }) test("a child that needs you keeps its parents past the limit", () => { const sessions = [ ...SESSIONS, session("deep", { parentID: "app-19", status: "question", requestSince: NOW - minute, activity: NOW - 30 * minute, }), ] const result = view({ sessions }, { subagents: true, show: "10" }) expect(keys(section(result.sections, "main"))?.slice(-2)).toEqual([key("app-19"), key("deep")]) }) test("familyRequests only gives roots their family's requests with Subagents off", () => { const sessions = [ session("root"), session("child", { parentID: "root", status: "question", requestSince: NOW - minute }), ] expect(familyRequests(sessions, false).get(key("root"))?.key).toBe(key("child")) expect(familyRequests(sessions, true).get(key("root"))).toBeUndefined() expect(familyRequests(sessions, true).get(key("child"))?.kind).toBe("question") }) test("Archived lists archived and hidden sessions last; one that needs you surfaces in place", () => { const sessions = replace( session("app-2", { archived: true }), session("app-4", { archived: true, status: "approval", requestSince: NOW - minute }), session("app-6"), ) const hiddenUntilReply = { [key("app-6")]: NOW - hour } const off = view({ sessions, hiddenUntilReply }, { show: "all" }) expect(off.order).not.toContain(key("app-2")) expect(off.order).not.toContain(key("app-6")) expect(section(off.sections, "main")?.rows.find((row) => row.key === key("app-4"))).toMatchObject({ surfaced: true, archived: false, }) const on = view({ sessions, hiddenUntilReply }, { show: "all", archived: true }) expect(on.sections.at(-1)?.id).toBe("archived") expect(keys(on.sections.at(-1))).toEqual([key("app-2"), key("app-6")]) expect(on.sections.at(-1)?.rows[0]).toMatchObject({ archived: true }) }) test("a collapsed section keeps its selected row and marks what it hides", () => { const sessions = replace( session("app-3", { status: "unread" }), session("web-2", { server: REMOTE, projectID: "web", status: "question", requestSince: NOW - minute }), ) const collapsed = { [`project:${key("app")}`]: true, [`project:${key("web", REMOTE)}`]: true } const result = view( { sessions, selected: key("app-8"), view: { filters: EMPTY_FILTERS, collapsed, expanded: {} } }, { groupBy: "project" }, ) const app = section(result.sections, `project:${key("app")}`) expect(app).toMatchObject({ collapsed: true, collapsible: true, more: 0, hiddenMark: "unread" }) expect(keys(app)).toEqual([key("app-8")]) expect(section(result.sections, `project:${key("web", REMOTE)}`)?.hiddenMark).toBe("attention") }) test("filters and search narrow sessions and drafts; filteredOut when nothing matches", () => { const filters = { ...EMPTY_FILTERS, query: "WEB-", server: [REMOTE], project: [key("web", REMOTE)] } const result = view({ drafts: [draft("d")], view: { filters, collapsed: {}, expanded: {} } }, { show: "all" }) expect(result.order).toHaveLength(8) expect(result.filteredOut).toBe(false) const none = view({ drafts: [draft("d")], view: { filters: { ...EMPTY_FILTERS, status: ["failed"] }, collapsed: {}, expanded: {} }, }) expect(none.sections).toEqual([]) expect(none.filteredOut).toBe(true) expect(none.empty).toBe(false) const loose = view({ view: { filters: { ...EMPTY_FILTERS, project: ["none"] }, collapsed: {}, expanded: {} } }) expect(loose.order).toEqual([key("loose")]) }) test("empty when there is no session or draft", () => { const result = view({ sessions: [] }) expect(result).toEqual({ sections: [], order: [], empty: true, filteredOut: false }) }) test("rows know whether they are open, pinned and selected", () => { const result = view({ open: [key("app-1")], pins: [key("app-2")], selected: key("app-2") }) expect(section(result.sections, "pinned")?.rows[0]).toMatchObject({ pinned: true, selected: true, open: false }) expect(section(result.sections, "main")?.rows[1]).toMatchObject({ key: key("app-1"), open: true, pinned: false }) }) })