|
| 1 | +/** |
| 2 | + * Archived-instances subsection regression: the profile panel's Instances list |
| 3 | + * splits live and relay-archived siblings, rendering archived rows under a |
| 4 | + * clearly labeled "Archived" header so unarchive stays UI-reachable for |
| 5 | + * channel-less agents. The section appears whenever live OR archived instances |
| 6 | + * exist, self-omits when neither does, shows only the Archived subsection for |
| 7 | + * an all-archived persona, and archived rows keep the deliberate explicit- |
| 8 | + * pubkey click that feeds selector matrix rule 3. |
| 9 | + * |
| 10 | + * Mounts the shipping ProfileInstancesSection (owned by the Runtime tab) and |
| 11 | + * drives the real expand toggle rather than reimplementing it. |
| 12 | + */ |
| 13 | + |
| 14 | +import assert from "node:assert/strict"; |
| 15 | +import { after, afterEach, before, test } from "node:test"; |
| 16 | + |
| 17 | +import { JSDOM } from "jsdom"; |
| 18 | + |
| 19 | +const dom = new JSDOM("<!doctype html><html><body></body></html>", { |
| 20 | + url: "http://localhost", |
| 21 | +}); |
| 22 | + |
| 23 | +let cleanup; |
| 24 | +let fireEvent; |
| 25 | +let render; |
| 26 | +let screen; |
| 27 | +let createElement; |
| 28 | +let ProfileInstancesSection; |
| 29 | + |
| 30 | +const LIVE_PK = "b".repeat(64); |
| 31 | +const SECOND_LIVE_PK = "c".repeat(64); |
| 32 | +const ARCHIVED_PK = "a".repeat(64); |
| 33 | + |
| 34 | +function agent(overrides = {}) { |
| 35 | + return { |
| 36 | + pubkey: LIVE_PK, |
| 37 | + name: "Instance", |
| 38 | + personaId: "persona-1", |
| 39 | + status: "stopped", |
| 40 | + ...overrides, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +function baseProps(overrides = {}) { |
| 45 | + return { |
| 46 | + currentPubkey: null, |
| 47 | + instances: [], |
| 48 | + archivedInstances: [], |
| 49 | + onOpenInstance: () => {}, |
| 50 | + ...overrides, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +function renderSection(props) { |
| 55 | + return render(createElement(ProfileInstancesSection, baseProps(props))); |
| 56 | +} |
| 57 | + |
| 58 | +before(async () => { |
| 59 | + Object.assign(globalThis, { |
| 60 | + document: dom.window.document, |
| 61 | + HTMLElement: dom.window.HTMLElement, |
| 62 | + window: dom.window, |
| 63 | + IS_REACT_ACT_ENVIRONMENT: true, |
| 64 | + }); |
| 65 | + Object.defineProperty(globalThis, "navigator", { |
| 66 | + configurable: true, |
| 67 | + value: dom.window.navigator, |
| 68 | + writable: true, |
| 69 | + }); |
| 70 | + dom.window.matchMedia = () => ({ |
| 71 | + matches: true, |
| 72 | + addEventListener() {}, |
| 73 | + removeEventListener() {}, |
| 74 | + }); |
| 75 | + |
| 76 | + ({ cleanup, fireEvent, render, screen } = await import( |
| 77 | + "@testing-library/react" |
| 78 | + )); |
| 79 | + ({ createElement } = await import("react")); |
| 80 | + ({ ProfileInstancesSection } = await import("./ProfileInstancesSection.tsx")); |
| 81 | +}); |
| 82 | + |
| 83 | +afterEach(() => cleanup?.()); |
| 84 | +after(() => dom.window.close()); |
| 85 | + |
| 86 | +test("test_archived_and_live_instances_render_archived_subsection", () => { |
| 87 | + renderSection({ |
| 88 | + instances: [agent({ pubkey: LIVE_PK, name: "Live" })], |
| 89 | + archivedInstances: [agent({ pubkey: ARCHIVED_PK, name: "Archived one" })], |
| 90 | + }); |
| 91 | + fireEvent.click(screen.getByTestId("user-profile-instances")); |
| 92 | + |
| 93 | + assert.equal( |
| 94 | + screen.getByTestId("user-profile-instances").textContent, |
| 95 | + "2 instances", |
| 96 | + ); |
| 97 | + assert.ok(screen.getByTestId("user-profile-instances-archived-header")); |
| 98 | + assert.ok(screen.getByTestId(`user-profile-instance-${LIVE_PK}`)); |
| 99 | + assert.ok(screen.getByTestId(`user-profile-instance-${ARCHIVED_PK}`)); |
| 100 | +}); |
| 101 | + |
| 102 | +test("test_no_archived_instances_omits_archived_subsection", () => { |
| 103 | + renderSection({ |
| 104 | + instances: [ |
| 105 | + agent({ pubkey: LIVE_PK, name: "Live" }), |
| 106 | + agent({ pubkey: SECOND_LIVE_PK, name: "Live two" }), |
| 107 | + ], |
| 108 | + archivedInstances: [], |
| 109 | + }); |
| 110 | + fireEvent.click(screen.getByTestId("user-profile-instances")); |
| 111 | + |
| 112 | + assert.equal(screen.queryByTestId("user-profile-instances-archived"), null); |
| 113 | + assert.ok(screen.getByTestId(`user-profile-instance-${LIVE_PK}`)); |
| 114 | +}); |
| 115 | + |
| 116 | +test("test_all_archived_persona_shows_only_archived_subsection", () => { |
| 117 | + renderSection({ |
| 118 | + instances: [], |
| 119 | + archivedInstances: [agent({ pubkey: ARCHIVED_PK, name: "Archived only" })], |
| 120 | + }); |
| 121 | + // Section renders even with an empty live list. |
| 122 | + fireEvent.click(screen.getByTestId("user-profile-instances")); |
| 123 | + |
| 124 | + assert.equal( |
| 125 | + screen.getByTestId("user-profile-instances").textContent, |
| 126 | + "1 instance", |
| 127 | + ); |
| 128 | + assert.ok(screen.getByTestId("user-profile-instances-archived-header")); |
| 129 | + assert.ok(screen.getByTestId(`user-profile-instance-${ARCHIVED_PK}`)); |
| 130 | +}); |
| 131 | + |
| 132 | +test("test_no_instances_at_all_omits_instances_section", () => { |
| 133 | + renderSection({ instances: [], archivedInstances: [] }); |
| 134 | + assert.equal(screen.queryByTestId("user-profile-instances-section"), null); |
| 135 | +}); |
| 136 | + |
| 137 | +test("test_archived_row_click_opens_that_explicit_pubkey", () => { |
| 138 | + const opened = []; |
| 139 | + renderSection({ |
| 140 | + instances: [], |
| 141 | + archivedInstances: [agent({ pubkey: ARCHIVED_PK, name: "Archived only" })], |
| 142 | + onOpenInstance: (pubkey) => opened.push(pubkey), |
| 143 | + }); |
| 144 | + fireEvent.click(screen.getByTestId("user-profile-instances")); |
| 145 | + fireEvent.click(screen.getByTestId(`user-profile-instance-${ARCHIVED_PK}`)); |
| 146 | + |
| 147 | + // The archived row keeps the deliberate explicit-pubkey path (unarchive). |
| 148 | + assert.deepEqual(opened, [ARCHIVED_PK]); |
| 149 | +}); |
0 commit comments