|
| 1 | +import type { Run } from '../store'; |
| 2 | +import type { TestCase, AnyTest } from '@swedevtools/livedoc-schema'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Converts a title string to a URL-friendly slug. |
| 6 | + * e.g. "Browser launches & provides valid Playwright objects" → "browser-launches-provides-valid-playwright-objects" |
| 7 | + */ |
| 8 | +export function toSlug(title: string): string { |
| 9 | + return title |
| 10 | + .toLowerCase() |
| 11 | + .replace(/['']/g, '') // remove apostrophes |
| 12 | + .replace(/[^a-z0-9]+/g, '-') // replace non-alphanumeric runs with hyphens |
| 13 | + .replace(/^-+|-+$/g, ''); // trim leading/trailing hyphens |
| 14 | +} |
| 15 | + |
| 16 | +// ── Build hash from current view state ────────────────────────── |
| 17 | + |
| 18 | +export interface DeepLinkView { |
| 19 | + type: 'summary' | 'node' | 'group'; |
| 20 | + id?: string; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Builds a URL hash string from the current navigation state. |
| 25 | + * Returns '' for the summary (home) view. |
| 26 | + */ |
| 27 | +export function buildHash(view: DeepLinkView, run: Run | undefined): string { |
| 28 | + if (!run || view.type === 'summary') return ''; |
| 29 | + |
| 30 | + if (view.type === 'group' && view.id) { |
| 31 | + // Group ids are "group:path/segments" — extract the path |
| 32 | + const path = view.id.replace(/^group:/, ''); |
| 33 | + return `#/group/${path}`; |
| 34 | + } |
| 35 | + |
| 36 | + if (view.type === 'node' && view.id) { |
| 37 | + const item = run.itemById[view.id]; |
| 38 | + if (!item) return ''; |
| 39 | + |
| 40 | + // Check if this is a top-level document (TestCase/Feature/Specification) |
| 41 | + const doc = findDocumentForItem(run, view.id); |
| 42 | + if (!doc) return ''; |
| 43 | + |
| 44 | + if (doc.id === view.id) { |
| 45 | + // Navigating to a document itself |
| 46 | + return `#/${toSlug(doc.title)}`; |
| 47 | + } |
| 48 | + |
| 49 | + // Navigating to a child test within a document |
| 50 | + return `#/${toSlug(doc.title)}/${toSlug(item.title)}`; |
| 51 | + } |
| 52 | + |
| 53 | + return ''; |
| 54 | +} |
| 55 | + |
| 56 | +// ── Resolve hash back to a view ───────────────────────────────── |
| 57 | + |
| 58 | +export interface ResolvedView { |
| 59 | + type: 'summary' | 'node' | 'group'; |
| 60 | + id?: string; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Parses a URL hash and resolves it to a navigation view. |
| 65 | + * Returns null if the hash can't be resolved (item not found). |
| 66 | + */ |
| 67 | +export function resolveHash(hash: string, run: Run | undefined): ResolvedView | null { |
| 68 | + if (!hash || hash === '#' || hash === '#/') { |
| 69 | + return { type: 'summary' }; |
| 70 | + } |
| 71 | + |
| 72 | + // Strip leading #/ |
| 73 | + const path = hash.replace(/^#\/?/, ''); |
| 74 | + if (!path) return { type: 'summary' }; |
| 75 | + |
| 76 | + // Group paths: /group/path/segments |
| 77 | + if (path.startsWith('group/')) { |
| 78 | + const groupPath = path.slice('group/'.length); |
| 79 | + return { type: 'group', id: `group:${groupPath}` }; |
| 80 | + } |
| 81 | + |
| 82 | + if (!run) return null; // Data not loaded yet |
| 83 | + |
| 84 | + const segments = path.split('/').filter(Boolean); |
| 85 | + |
| 86 | + if (segments.length === 1) { |
| 87 | + // Document-level slug |
| 88 | + const docSlug = segments[0]; |
| 89 | + const doc = findDocumentBySlug(run, docSlug); |
| 90 | + if (doc) return { type: 'node', id: doc.id }; |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + if (segments.length === 2) { |
| 95 | + // Document/test slug |
| 96 | + const [docSlug, testSlug] = segments; |
| 97 | + const doc = findDocumentBySlug(run, docSlug); |
| 98 | + if (!doc) return null; |
| 99 | + |
| 100 | + const test = findTestBySlug(doc, testSlug); |
| 101 | + if (test) return { type: 'node', id: test.id }; |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + return null; |
| 106 | +} |
| 107 | + |
| 108 | +// ── Lookup helpers ────────────────────────────────────────────── |
| 109 | + |
| 110 | +function findDocumentForItem(run: Run, itemId: string): TestCase | undefined { |
| 111 | + for (const doc of run.run.documents ?? []) { |
| 112 | + if (doc.id === itemId) return doc; |
| 113 | + if (hasDescendant(doc, itemId)) return doc; |
| 114 | + } |
| 115 | + return undefined; |
| 116 | +} |
| 117 | + |
| 118 | +function hasDescendant(doc: TestCase, itemId: string): boolean { |
| 119 | + for (const test of doc.tests ?? []) { |
| 120 | + if (test.id === itemId) return true; |
| 121 | + if ('steps' in test && Array.isArray((test as any).steps)) { |
| 122 | + for (const step of (test as any).steps as AnyTest[]) { |
| 123 | + if (step.id === itemId) return true; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return false; |
| 128 | +} |
| 129 | + |
| 130 | +function findDocumentBySlug(run: Run, slug: string): TestCase | undefined { |
| 131 | + return (run.run.documents ?? []).find(doc => toSlug(doc.title) === slug); |
| 132 | +} |
| 133 | + |
| 134 | +function findTestBySlug(doc: TestCase, slug: string): AnyTest | undefined { |
| 135 | + for (const test of doc.tests ?? []) { |
| 136 | + if (toSlug(test.title) === slug) return test; |
| 137 | + // Check nested steps/rules |
| 138 | + if ('steps' in test && Array.isArray((test as any).steps)) { |
| 139 | + for (const step of (test as any).steps as AnyTest[]) { |
| 140 | + if (toSlug(step.title) === slug) return step; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + return undefined; |
| 145 | +} |
0 commit comments