-
Notifications
You must be signed in to change notification settings - Fork 782
fix(viewer): handle sessions missing ids #366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
honor2030
wants to merge
2
commits into
rohitg00:main
Choose a base branch
from
honor2030:fix/viewer-missing-session-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+235
−19
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| import * as vm from "node:vm"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { renderViewerDocument } from "../src/viewer/document.js"; | ||
|
|
||
| function htmlEscape(value: string): string { | ||
| return value | ||
| .replace(/&/g, "&") | ||
| .replace(/</g, "<") | ||
| .replace(/>/g, ">") | ||
| .replace(/"/g, """); | ||
| } | ||
|
|
||
| function loadViewerSandbox() { | ||
| const rendered = renderViewerDocument(); | ||
| expect(rendered.found).toBe(true); | ||
| if (!rendered.found) throw new Error("viewer document not found"); | ||
|
|
||
| const scriptMatch = rendered.html.match(/<script nonce="[^"]+">([\s\S]*?)<\/script>/); | ||
| expect(scriptMatch).not.toBeNull(); | ||
| if (!scriptMatch) throw new Error("viewer script not found"); | ||
|
|
||
| const elements = new Map<string, any>(); | ||
| const createMockElement = (id = "") => { | ||
| const attributes = new Map<string, string>(); | ||
| const classes = new Set<string>(); | ||
| const listeners = new Map<string, Array<(event?: unknown) => void>>(); | ||
| return { | ||
| id, | ||
| innerHTML: "", | ||
| textContent: "", | ||
| value: "", | ||
| checked: false, | ||
| dataset: {}, | ||
| style: {}, | ||
| listeners, | ||
| classList: { | ||
| add: (name: string) => classes.add(name), | ||
| remove: (name: string) => classes.delete(name), | ||
| contains: (name: string) => classes.has(name), | ||
| toggle: (name: string, force?: boolean) => { | ||
| const enabled = force ?? !classes.has(name); | ||
| if (enabled) classes.add(name); | ||
| else classes.delete(name); | ||
| return enabled; | ||
| }, | ||
| }, | ||
| addEventListener: (type: string, handler: (event?: unknown) => void) => { | ||
| const current = listeners.get(type) || []; | ||
| current.push(handler); | ||
| listeners.set(type, current); | ||
| }, | ||
| getAttribute: (name: string) => attributes.get(name) ?? null, | ||
| setAttribute: (name: string, value: unknown) => { | ||
| attributes.set(name, String(value)); | ||
| }, | ||
| querySelectorAll: () => [], | ||
| }; | ||
| }; | ||
| const getElement = (id: string) => { | ||
| if (!elements.has(id)) elements.set(id, createMockElement(id)); | ||
| return elements.get(id); | ||
| }; | ||
|
|
||
| const tabs = [ | ||
| "dashboard", | ||
| "graph", | ||
| "memories", | ||
| "timeline", | ||
| "sessions", | ||
| "lessons", | ||
| "actions", | ||
| "crystals", | ||
| "audit", | ||
| "activity", | ||
| "profile", | ||
| "replay", | ||
| ]; | ||
| const tabButtons = tabs.map((tab) => ({ ...createMockElement(), dataset: { tab } })); | ||
| const views = tabs.map((tab) => ({ ...createMockElement(`view-${tab}`), id: `view-${tab}` })); | ||
| const checkboxes = [createMockElement(), createMockElement()].map((el) => ({ ...el, checked: false })); | ||
| const querySelectorAll = (selector: string) => { | ||
| if (selector === ".tab-bar button") return tabButtons; | ||
| if (selector === ".view") return views; | ||
| if (selector === 'input[type="checkbox"]') return checkboxes; | ||
| return []; | ||
| }; | ||
|
|
||
| const document = { | ||
| documentElement: { dataset: {} }, | ||
| createElement: () => { | ||
| let text = ""; | ||
| return { | ||
| set textContent(value: unknown) { | ||
| text = String(value ?? ""); | ||
| }, | ||
| get innerHTML() { | ||
| return htmlEscape(text); | ||
| }, | ||
| }; | ||
| }, | ||
| getElementById: getElement, | ||
| querySelectorAll, | ||
| addEventListener: () => {}, | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const sandbox: Record<string, any> = { | ||
| console: { log: () => {}, warn: () => {}, error: () => {} }, | ||
| document, | ||
| window: { | ||
| location: { | ||
| search: "", | ||
| port: "3113", | ||
| protocol: "http:", | ||
| hostname: "localhost", | ||
| host: "localhost:3113", | ||
| origin: "http://localhost:3113", | ||
| }, | ||
| matchMedia: () => ({ matches: false }), | ||
| addEventListener: () => {}, | ||
| }, | ||
| localStorage: { getItem: () => null, setItem: () => {} }, | ||
| fetch: async () => ({ ok: true, json: async () => ({}) }), | ||
| WebSocket: function WebSocket() {}, | ||
| navigator: { userAgent: "vitest" }, | ||
| Element: function Element() {}, | ||
| alert: () => {}, | ||
| setInterval: () => 0, | ||
| clearInterval: () => {}, | ||
| setTimeout: () => 0, | ||
| clearTimeout: () => {}, | ||
| URLSearchParams, | ||
| Date, | ||
| Math, | ||
| Promise, | ||
| JSON, | ||
| Array, | ||
| Object, | ||
| String, | ||
| Number, | ||
| parseInt, | ||
| encodeURIComponent, | ||
| }; | ||
|
|
||
| const scriptWithoutAutoStart = scriptMatch[1].replace( | ||
| /\n\s*loadTab\('dashboard'\);\n\s*connectWs\(\);\n\s*startDashboardAutoRefresh\(\);\s*$/, | ||
| "\n", | ||
| ); | ||
|
|
||
| vm.createContext(sandbox); | ||
| vm.runInContext(scriptWithoutAutoStart, sandbox); | ||
|
|
||
| return { sandbox, getElement }; | ||
| } | ||
|
|
||
| describe("viewer session rendering", () => { | ||
| it("does not throw when dashboard sessions are missing ids", () => { | ||
| const { sandbox, getElement } = loadViewerSandbox(); | ||
| sandbox.state.dashboard = { | ||
| loaded: true, | ||
| health: { status: "healthy", health: {} }, | ||
| sessions: [{ status: "active", observationCount: 3, startedAt: "2026-05-13T12:00:00Z" }], | ||
| memories: [], | ||
| graphStats: null, | ||
| recentAudit: [], | ||
| lessons: [], | ||
| crystals: [], | ||
| }; | ||
|
|
||
| expect(() => sandbox.renderDashboard()).not.toThrow(); | ||
| expect(getElement("view-dashboard").innerHTML).toContain("Unknown session"); | ||
| }); | ||
|
|
||
| it("does not throw when timeline and sessions tabs receive sessions missing ids", () => { | ||
| const { sandbox, getElement } = loadViewerSandbox(); | ||
| const sessions = [{ status: "active", observationCount: 1, startedAt: "2026-05-13T12:00:00Z" }]; | ||
|
|
||
| expect(() => sandbox.renderTimelineToolbar(sessions)).not.toThrow(); | ||
| expect(getElement("view-timeline").innerHTML).toContain("Unknown session"); | ||
|
|
||
| sandbox.state.sessions.items = sessions; | ||
| expect(() => sandbox.renderSessions()).not.toThrow(); | ||
| expect(getElement("view-sessions").innerHTML).toContain("Unknown session"); | ||
|
|
||
| const tabButtons = sandbox.document.querySelectorAll(".tab-bar button"); | ||
| expect(tabButtons.length).toBeGreaterThan(0); | ||
| expect(() => sandbox.switchTab("sessions")).not.toThrow(); | ||
| expect(tabButtons.some((button: any) => button.classList.contains("active"))).toBe(true); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.