-
-
Notifications
You must be signed in to change notification settings - Fork 447
feat(web): mermaid diagram lightbox on click #741
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
heavygee
wants to merge
22
commits into
tiann:main
Choose a base branch
from
heavygee:feat/mermaid-lightbox-737
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.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c9aca67
feat(web): mermaid diagram lightbox on click
heavygee 254983b
fix(web): fit mermaid lightbox to viewport on open
heavygee 64cfe2b
fix(web): fit mermaid lightbox to device screen not inner panel
heavygee 02af943
fix(web): show mermaid lightbox by reusing inline SVG
heavygee 4876f66
fix(web): uniquify mermaid SVG ids in lightbox clone
heavygee d5a0cd4
fix(web): give mermaid lightbox SVG explicit dimensions
heavygee bb2a624
fix(web): render mermaid lightbox via isolated SVG data URL
heavygee 7883a81
fix(web): lightbox re-renders SVG for sequence diagrams
heavygee ddedc46
fix(web): mermaid lightbox uses inline SVG in shadow DOM
heavygee c345f80
test(web): Playwright lightbox coverage per mermaid diagram type
heavygee c17296c
test(web): bounded Playwright via webServer, fix gantt fit sizing
heavygee f4b4717
chore(web): gitignore Playwright test-results
heavygee c390066
fix(web): address PR 741 bot feedback (typecheck, fit floor, gitignore)
heavygee cc673a8
test(web): Playwright asserts click expands diagram vs inline
heavygee cbef8c0
test(web): Playwright against live HAPI session for mermaid lightbox
heavygee a2dec2f
fix(web): undo wrapper transform in lightbox fit; carry fit floor in …
heavygee 9986410
fix(scripts): mermaid seed refuses to wipe non-fixture sessions
heavygee a7e7e24
fix(web): normalize mermaid svg for lightbox shadow root
heavygee 5869e7f
fix(web): keep mermaid lightbox content below the toolbar
heavygee b884b20
fix(web): guard ResizeObserver before constructing it
heavygee 14cb8d0
fix(scripts): live mermaid playwright wrapper runs from repo root
heavygee e70aa66
fix(web): accept signed viewBox values in mermaid lightbox normalize
heavygee 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,56 @@ | ||
| # Mermaid lightbox dogfood (Playwright) | ||
|
|
||
| Two Playwright targets: | ||
|
|
||
| | Target | What it exercises | Command | | ||
| |--------|-------------------|---------| | ||
| | **Component (Vite)** | `MermaidDiagram` in isolation on dev server | `npm run test:mermaid-lightbox:playwright` | | ||
| | **Live session (hub)** | Real chat thread, click-to-zoom | `npm run test:mermaid-lightbox:live` | | ||
|
|
||
| ## Live session (production-shaped) | ||
|
|
||
| **Session URL (after seed):** | ||
|
|
||
| `{HAPI_URL}/sessions/a7370000-0000-4000-8000-000000000737` | ||
|
|
||
| Default `HAPI_URL` for live tests: `http://127.0.0.1:3006` (daily driver). | ||
| For tailnet: `HAPI_URL=https://hapi.tail9944ee.ts.net` (seed **that** hub's DB first). | ||
|
|
||
| ### 1. Seed fixtures (hub DB) | ||
|
|
||
| On the machine that owns `HAPI_DB_PATH` (usually `~/.hapi/hapi.db`): | ||
|
|
||
| ```bash | ||
| bun run seed:mermaid-lightbox:session | ||
| ``` | ||
|
|
||
| Inserts 15 assistant messages (one per diagram type). Re-run to replace messages in that session. | ||
|
|
||
| ### 2. Deploy web with your branch | ||
|
|
||
| ```bash | ||
| hapi-driver-rebuild --build-web | ||
| # activate soup when ready (restarts hub) | ||
| ``` | ||
|
|
||
| Hard-refresh the browser after web changes. | ||
|
|
||
| ### 3. Run live Playwright | ||
|
|
||
| ```bash | ||
| HAPI_LIVE=1 HAPI_URL=http://127.0.0.1:3006 npm run test:mermaid-lightbox:live | ||
| ``` | ||
|
|
||
| Requires `~/.hapi/settings.json` `cliApiToken` (or `HAPI_ACCESS_TOKEN`). | ||
|
|
||
| **Pass criteria:** dialog opens, SVG in **shadow root** (`[data-mermaid-lightbox]`), expands vs inline, sequence has multiple actors/lines. | ||
|
|
||
| If tests report `legacy` or `empty` lightbox, the served web bundle predates the shadow-DOM fix — rebuild driver. | ||
|
|
||
| ## Isolation page (not chat) | ||
|
|
||
| Only for component regression; **not** the same as chat: | ||
|
|
||
| `http://127.0.0.1:5173/mermaid-lightbox-e2e.html?case=sequence` (Vite dev, not on tailnet dist unless you add the HTML to a build). | ||
|
|
||
| Diagram sources: `web/src/dev/mermaid-lightbox-cases.ts` |
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,20 @@ | ||
| #!/usr/bin/env node | ||
| /** Bounded wrapper: Playwright against a real HAPI chat session (no Vite). */ | ||
| import { spawnSync } from 'node:child_process' | ||
| import { dirname, resolve } from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
|
|
||
| const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../..') | ||
| const npmBin = process.env.NPM_BIN ?? 'npm' | ||
|
|
||
| const result = spawnSync( | ||
| npmBin, | ||
| ['run', 'test:mermaid-lightbox:live'], | ||
| { | ||
| cwd: REPO_ROOT, | ||
| stdio: 'inherit', | ||
| env: { ...process.env, PATH: process.env.PATH, HAPI_LIVE: '1' }, | ||
| }, | ||
| ) | ||
|
|
||
| process.exit(result.status === null ? 1 : result.status) |
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,26 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Bounded wrapper for mermaid lightbox Playwright (web/e2e). | ||
| * Vite lifecycle is owned by web/playwright.config.ts webServer — not this process. | ||
| * | ||
| * Usage (from repo root): | ||
| * npm run test:mermaid-lightbox:playwright | ||
| */ | ||
| import { spawnSync } from 'node:child_process' | ||
| import { dirname, resolve } from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
|
|
||
| const WEB_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '../../web') | ||
| const npmBin = process.env.NPM_BIN ?? 'npm' | ||
|
|
||
| const result = spawnSync( | ||
| npmBin, | ||
| ['run', 'test:mermaid-lightbox:e2e'], | ||
| { | ||
| cwd: WEB_DIR, | ||
| stdio: 'inherit', | ||
| env: { ...process.env, PATH: process.env.PATH }, | ||
| }, | ||
| ) | ||
|
|
||
| process.exit(result.status === null ? 1 : result.status) |
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,85 @@ | ||
| /** | ||
| * Seed assistant messages with mermaid fixtures into a hub SQLite DB. | ||
| * Run on the host that owns HAPI_DB_PATH (usually the hub machine). | ||
| * | ||
| * HAPI_DB_PATH=~/.hapi/hapi.db SESSION_ID=<uuid> bun run scripts/dev/mermaid-lightbox-seed-session-db.ts | ||
| */ | ||
| import { Database } from 'bun:sqlite' | ||
| import { randomUUID } from 'node:crypto' | ||
| import { homedir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { | ||
| MERMAID_LIGHTBOX_CASE_IDS, | ||
| MERMAID_LIGHTBOX_CASES, | ||
| } from '../../web/src/dev/mermaid-lightbox-cases' | ||
|
|
||
| const dbPath = process.env.HAPI_DB_PATH ?? join(homedir(), '.hapi', 'hapi.db') | ||
| /** Stable id for mermaid Playwright live session (create if missing). */ | ||
| const sessionId = process.env.SESSION_ID ?? 'a7370000-0000-4000-8000-000000000737' | ||
| const sessionTag = 'mermaid-lightbox-e2e' | ||
|
|
||
| function agentMermaidEnvelope(caseId: string, code: string) { | ||
| const text = `<!-- mermaid-e2e:${caseId} -->\n\`\`\`mermaid\n${code.trim()}\n\`\`\`` | ||
| return { | ||
| role: 'agent', | ||
| content: { | ||
| type: 'output', | ||
| data: { | ||
| type: 'assistant', | ||
| uuid: randomUUID(), | ||
| parentUuid: null, | ||
| isSidechain: false, | ||
| message: { | ||
| content: [{ type: 'text', text }], | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| const db = new Database(dbPath) | ||
| const now = Date.now() | ||
| const existing = db.prepare('SELECT id, tag FROM sessions WHERE id = ?').get(sessionId) as | ||
| | { id: string; tag: string | null } | ||
| | undefined | ||
|
|
||
| if (existing && existing.tag !== sessionTag) { | ||
| throw new Error( | ||
| `Refusing to seed mermaid fixtures into session ${sessionId}: tag is ` | ||
| + `${JSON.stringify(existing.tag)}, expected ${JSON.stringify(sessionTag)}. ` | ||
| + `Unset SESSION_ID or use a session created by this script.`, | ||
| ) | ||
| } | ||
|
|
||
| if (!existing) { | ||
| db.prepare(` | ||
| INSERT INTO sessions ( | ||
| id, tag, namespace, created_at, updated_at, active, seq | ||
| ) VALUES (?, ?, 'default', ?, ?, 0, 0) | ||
| `).run(sessionId, sessionTag, now, now) | ||
| console.log(`created session ${sessionId} (${sessionTag})`) | ||
| } | ||
|
|
||
| const insert = db.prepare(` | ||
| INSERT INTO messages (id, session_id, content, created_at, seq, local_id, invoked_at, scheduled_at) | ||
| VALUES (?, ?, ?, ?, ?, NULL, ?, NULL) | ||
| `) | ||
|
|
||
| db.prepare('DELETE FROM messages WHERE session_id = ?').run(sessionId) | ||
|
|
||
| let seqRow = db.prepare('SELECT COALESCE(MAX(seq), 0) AS maxSeq FROM messages WHERE session_id = ?').get(sessionId) as { | ||
| maxSeq: number | ||
| } | ||
|
|
||
| for (const caseId of MERMAID_LIGHTBOX_CASE_IDS) { | ||
| const code = MERMAID_LIGHTBOX_CASES[caseId] | ||
| const envelope = agentMermaidEnvelope(caseId, code) | ||
| const seq = (seqRow.maxSeq ?? 0) + 1 | ||
| seqRow = { maxSeq: seq } | ||
| const messageId = randomUUID() | ||
| insert.run(messageId, sessionId, JSON.stringify(envelope), now, seq, now) | ||
| console.log(`seeded ${caseId} @ seq ${seq}`) | ||
| } | ||
|
|
||
| db.prepare('UPDATE sessions SET updated_at = ? WHERE id = ?').run(now, sessionId) | ||
| console.log(`Done. Open: /sessions/${sessionId}`) | ||
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| node_modules/ | ||
| dist/ | ||
| dev-dist/ | ||
| test-results/ |
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,82 @@ | ||
| import { readFileSync } from 'node:fs' | ||
| import { homedir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import type { Page } from '@playwright/test' | ||
|
|
||
| export function getHapiBaseUrl(): string { | ||
| return (process.env.HAPI_URL ?? 'http://127.0.0.1:3006').replace(/\/$/, '') | ||
| } | ||
|
|
||
| export function getMermaidTestSessionId(): string { | ||
| return process.env.SESSION_ID ?? 'a7370000-0000-4000-8000-000000000737' | ||
| } | ||
|
|
||
| export function readCliAccessToken(): string { | ||
| if (process.env.HAPI_ACCESS_TOKEN?.trim()) { | ||
| return process.env.HAPI_ACCESS_TOKEN.trim() | ||
| } | ||
| const settingsPath = process.env.HAPI_SETTINGS_PATH ?? join(homedir(), '.hapi', 'settings.json') | ||
| const settings = JSON.parse(readFileSync(settingsPath, 'utf8')) as { cliApiToken?: string } | ||
| if (!settings.cliApiToken) { | ||
| throw new Error(`Missing cliApiToken in ${settingsPath}`) | ||
| } | ||
| return settings.cliApiToken | ||
| } | ||
|
|
||
| export async function installHapiAuth(page: Page, baseUrl: string, accessToken: string) { | ||
| await page.addInitScript(({ token, url }) => { | ||
| localStorage.setItem(`hapi_access_token::${url}`, token) | ||
| }, { token: accessToken, url: baseUrl }) | ||
| } | ||
|
|
||
| export async function scrollChatToBottom(page: Page) { | ||
| for (let i = 0; i < 24; i += 1) { | ||
| const found = await page.locator('[data-mermaid-diagram][data-rendered="true"]').count() | ||
| if (found > 0) break | ||
| await page.evaluate(() => { | ||
| const scrollers = [...document.querySelectorAll('*')].filter( | ||
| (el) => el.scrollHeight > el.clientHeight + 80, | ||
| ) | ||
| scrollers.sort((a, b) => b.scrollHeight - a.scrollHeight) | ||
| const target = scrollers[0] | ||
| if (target) target.scrollTop = target.scrollHeight | ||
| window.scrollTo(0, document.body.scrollHeight) | ||
| }) | ||
| await page.waitForTimeout(400) | ||
| } | ||
| } | ||
|
|
||
| export type LiveLightboxMetrics = { | ||
| inlineW: number | ||
| inlineH: number | ||
| lightboxW: number | ||
| lightboxH: number | ||
| hasShadowSvg: boolean | ||
| shapeTotal: number | ||
| coverage: number | ||
| } | ||
|
|
||
| export async function readLiveLightboxMetrics(page: Page): Promise<LiveLightboxMetrics> { | ||
| return page.evaluate(() => { | ||
| const inlineSvg = document.querySelector('[data-mermaid-diagram][data-rendered="true"] svg') | ||
| const inlineBox = inlineSvg?.getBoundingClientRect() | ||
| const host = document.querySelector('[data-mermaid-lightbox]') | ||
| const lightboxSvg = host?.shadowRoot?.querySelector('svg') | ||
| const lightboxBox = lightboxSvg?.getBoundingClientRect() | ||
| const vw = window.visualViewport?.width ?? window.innerWidth | ||
| const vh = window.visualViewport?.height ?? window.innerHeight | ||
| const shapes = | ||
| (lightboxSvg?.querySelectorAll('rect').length ?? 0) | ||
| + (lightboxSvg?.querySelectorAll('path').length ?? 0) | ||
| + (lightboxSvg?.querySelectorAll('line').length ?? 0) | ||
| return { | ||
| inlineW: inlineBox?.width ?? 0, | ||
| inlineH: inlineBox?.height ?? 0, | ||
| lightboxW: lightboxBox?.width ?? 0, | ||
| lightboxH: lightboxBox?.height ?? 0, | ||
| hasShadowSvg: Boolean(lightboxSvg), | ||
| shapeTotal: shapes, | ||
| coverage: Math.max((lightboxBox?.width ?? 0) / vw, (lightboxBox?.height ?? 0) / vh), | ||
| } | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.