import { test, expect } from '@playwright/test'; import { MOCK_FS_INIT_SCRIPT } from './fixtures/mock-fs-api.js'; import * as path from 'path'; import * as fs from 'fs/promises'; const HTML_PATH = path.resolve('browse/dist/browse.html'); test.describe('Browse', () => { test.beforeEach(async ({ page }) => { await page.addInitScript(MOCK_FS_INIT_SCRIPT); }); test('loads with the empty state visible and add-directory button enabled', async ({ page }) => { await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' }); await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 }); // file:// + no auto-server-root → the empty state is shown and the // browse table stays hidden until the user picks a directory. await expect(page.locator('#emptyState')).toBeVisible(); await expect(page.locator('#browseRoot')).toBeHidden(); const addDirBtn = page.locator('#addDirectoryBtn'); await expect(addDirBtn).toBeVisible(); await expect(addDirBtn).not.toBeDisabled(); }); test('renders rows from a mock directory after picking', async ({ page }) => { await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' }); await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 }); await page.evaluate(() => { window.__setMockDirectory('docs', [ { name: 'spec.pdf', content: '%PDF-fixture', size: 1024 }, { name: 'readme.md', content: '# Hello\n', size: 8 }, { name: 'notes.txt', content: 'note one\nnote two\n', size: 16 }, ]); }); await page.locator('#addDirectoryBtn').click(); // Browse swaps from empty state to the two-pane layout. The // tree pane is on the left; rows are
. await page.waitForSelector('#browseRoot:not(.hidden)', { timeout: 10000 }); await page.waitForFunction( () => document.querySelectorAll('#treeBody .tree-row').length >= 3, { timeout: 10000 } ); const rows = await page.locator('#treeBody .tree-row').count(); expect(rows).toBeGreaterThanOrEqual(3); // Right preview pane is present and starts in the empty state. await expect(page.locator('#previewPane')).toBeVisible(); await expect(page.locator('#previewBody')).toContainText(/Click a file/); }); test('clicking a file shows it in the preview pane', async ({ page }) => { await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' }); await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 }); await page.evaluate(() => { window.__setMockDirectory('docs', [ { name: 'notes.txt', content: 'hello world', size: 11 }, ]); }); await page.locator('#addDirectoryBtn').click(); await page.waitForSelector('#treeBody .tree-row', { timeout: 10000 }); // Click the file row. await page.locator('#treeBody .tree-row[data-isdir="false"]').first().click(); // Preview title updates to the file name; pop-out button appears. await expect(page.locator('#previewTitle')).toHaveText(/notes\.txt/); await expect(page.locator('#previewPopout')).toBeVisible(); }); test('clicking a .md file mounts the markdown editor with a TOC', async ({ page }) => { await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' }); await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 }); await page.evaluate(() => { window.__setMockDirectory('notes', [ { name: 'readme.md', content: '# Title\n\nIntro.\n\n## Section One\n\nText.\n\n### Subsection\n\nDeeper.', size: 100, }, ]); }); await page.locator('#addDirectoryBtn').click(); await page.waitForSelector('#treeBody .tree-row[data-isdir="false"]', { timeout: 10000 }); await page.locator('#treeBody .tree-row[data-isdir="false"]').first().click(); // Markdown plugin DOM mounts: shell, sidebar (front matter + // TOC), content (info header + editor). await expect(page.locator('.md-shell')).toBeVisible({ timeout: 15000 }); await expect(page.locator('.md-shell__sidebar')).toBeVisible(); await expect(page.locator('.md-shell__infohdr')).toBeVisible(); await expect(page.locator('.md-shell__editor')).toBeVisible(); // Outline lists the three headings. await page.waitForSelector('.md-toc__list .md-toc__item', { timeout: 10000 }); const tocItems = await page.locator('.md-toc__list .md-toc__item').allTextContents(); expect(tocItems).toEqual(['Title', 'Section One', 'Subsection']); // Source hint reflects local FS-API mode. await expect(page.locator('.md-shell__source')).toHaveText(/local/i); }); test('expands a .zip transmittal folder and previews a member', async ({ page }) => { await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' }); await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 }); // Build a real zip in-browser (JSZip is bundled into browse.html) // and hand its bytes to the mock FS as a single .zip file whose // name parses as a transmittal folder. await page.evaluate(async () => { const zip = new window.JSZip(); zip.file('DOC-001 (IFI) - Spec.pdf', '%PDF-1.4 fixture body'); zip.file('sub/note.txt', 'a note inside the zip'); const buf = await zip.generateAsync({ type: 'uint8array' }); window.__setMockDirectory('PartyA', [ { name: '2025-05-12_DOC-001 (IFI) - Title.zip', content: buf, size: buf.length }, ]); }); await page.locator('#addDirectoryBtn').click(); await page.waitForSelector('#treeBody .tree-row[data-iszip="true"]', { timeout: 10000 }); // Expand the .zip — it should list its members like a folder. await page.locator('#treeBody .tree-row[data-iszip="true"]').first().click(); await page.waitForFunction( () => document.querySelectorAll('#treeBody .tree-row').length >= 3, { timeout: 10000 } ); const labels = await page.locator('#treeBody .tree-name__label').allTextContents(); expect(labels.join('|')).toContain('DOC-001 (IFI) - Spec.pdf'); expect(labels.join('|')).toContain('sub'); // Drill into the subdir, then preview the text member. await page.locator('#treeBody .tree-row[data-isdir="true"]').last().click(); await page.waitForFunction( () => Array.from(document.querySelectorAll('#treeBody .tree-name__label')) .some(el => el.textContent === 'note.txt'), { timeout: 10000 } ); const noteRow = page.locator('#treeBody .tree-row', { has: page.locator('.tree-name__label', { hasText: /^note\.txt$/ }) }); await noteRow.click(); await expect(page.locator('#previewTitle')).toHaveText(/note\.txt/); await expect(page.locator('#previewBody')).toContainText('a note inside the zip'); }); test('Download (zip) bundles a folder via right-click → Download ZIP', async ({ page }) => { await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' }); await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 }); await page.evaluate(() => { window.__setMockDirectoryTree('mock-folder', { 'a.txt': 'AAA', 'sub': { 'b.txt': 'BBB', 'deep': { 'c.txt': 'CCC' }, '.zddc': 'acl: { permissions: { "*": r } }', // hidden — must not be in the zip '_template': { 'scaffold.txt': 'x' }, // hidden dir — must not be in the zip }, }); }); await page.locator('#addDirectoryBtn').click(); await page.waitForSelector('#browseRoot:not(.hidden)', { timeout: 10000 }); // Download ZIP lives in the row's right-click context menu — // the standalone toolbar button was retired when the context // menu became the canonical action surface (SPA overhaul, // commit 94b2e29). The picked-root folder doesn't render as // a row (only its CONTENTS do), so we test the next-level // folder: right-click sub/, download sub.zip. const subRow = page.locator('.tree-row', { has: page.locator('.tree-name__label', { hasText: /^sub$/ }) }); await subRow.waitFor({ state: 'visible', timeout: 5000 }); const [download] = await Promise.all([ page.waitForEvent('download'), (async () => { await subRow.click({ button: 'right' }); await page.locator('.zddc-menu__item', { hasText: 'Download ZIP' }) .first() .click(); })(), ]); expect(download.suggestedFilename()).toBe('sub.zip'); const file = await download.path(); const buf = await fs.readFile(file); // Valid zip: starts with the local-file-header magic, non-trivial. expect(buf.length).toBeGreaterThan(50); expect(buf.subarray(0, 4)).toEqual(Buffer.from([0x50, 0x4b, 0x03, 0x04])); // Introspect the entries with the bundled JSZip (in-page). const b64 = buf.toString('base64'); const entries = await page.evaluate(async (b64) => { const bin = atob(b64); const bytes = new Uint8Array(bin.length); for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); const z = await window.JSZip.loadAsync(bytes); return Object.keys(z.files).filter((n) => !z.files[n].dir).sort(); }, b64); expect(entries).toEqual([ 'sub/b.txt', 'sub/deep/c.txt', ]); }); }); // ── Menu harmonization: context-correct, capability/tier-driven ────────── test.describe('Browse menu — context & tiers', () => { test.beforeEach(async ({ page }) => { await page.addInitScript(MOCK_FS_INIT_SCRIPT); }); async function openWithTree(page) { await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' }); await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 }); await page.evaluate(() => { window.__setMockDirectoryTree('mock-folder', { 'a.txt': 'AAA', 'sub': { 'b.txt': 'BBB' }, }); }); await page.locator('#addDirectoryBtn').click(); await page.waitForSelector('#browseRoot:not(.hidden)', { timeout: 10000 }); } test('file row OMITS New folder / New file (context-correct)', async ({ page }) => { await openWithTree(page); const fileRow = page.locator('.tree-row', { has: page.locator('.tree-name__label', { hasText: /^a\.txt$/ }) }); await fileRow.click({ button: 'right' }); await page.waitForSelector('.zddc-menu', { timeout: 5000 }); await expect(page.locator('.zddc-menu__item', { hasText: 'New folder' })).toHaveCount(0); await expect(page.locator('.zddc-menu__item', { hasText: 'New file' })).toHaveCount(0); // Copy path/name removed; Navigate into folded into Open. await expect(page.locator('.zddc-menu__item', { hasText: 'Copy path' })).toHaveCount(0); await expect(page.locator('.zddc-menu__item', { hasText: 'Navigate into' })).toHaveCount(0); }); test('folder row SHOWS New folder (FS mode → create permitted), enabled', async ({ page }) => { await openWithTree(page); const folderRow = page.locator('.tree-row', { has: page.locator('.tree-name__label', { hasText: /^sub$/ }) }); await folderRow.click({ button: 'right' }); await page.waitForSelector('.zddc-menu', { timeout: 5000 }); const item = page.locator('.zddc-menu__item', { hasText: 'New folder' }).first(); await expect(item).toBeVisible(); await expect(item).not.toHaveClass(/is-disabled/); }); test('permission-gated items are HIDDEN when not permitted, shown when permitted', async ({ page }) => { await openWithTree(page); // Pure-DOM unit over the declarative model in server mode. const res = await page.evaluate(() => { window.app.state.source = 'server'; function labels(verbs) { const node = { name: 'doc.md', ext: 'md', isDir: false, isZip: false, virtual: false, url: '/doc.md', verbs: verbs }; return window.app.modules.menuModel .buildRowItems(node, null, { path_verbs: verbs }) .filter((i) => i.label).map((i) => i.label); } return { ro: labels('r'), rwd: labels('rwd') }; }); // read-only → no Rename/Delete (hidden, not greyed) expect(res.ro).not.toContain('Rename…'); expect(res.ro).not.toContain('Delete…'); // read+write+delete → both present expect(res.rwd).toContain('Rename…'); expect(res.rwd).toContain('Delete…'); }); // New folder / New file are not toolbar buttons — they live in the // row/pane context menu (see the "keyboard menu key and kebab" test). test('toolbar Sort and Show-hidden drive state', async ({ page }) => { await openWithTree(page); await page.locator('#sortSelect').selectOption('date:-1'); expect(await page.evaluate(() => window.app.state.sort)).toEqual({ key: 'date', dir: -1 }); await page.locator('#showHiddenChk').check(); expect(await page.evaluate(() => window.app.state.showHidden)).toBe(true); }); test('keyboard menu key and kebab both open the row menu', async ({ page }) => { await openWithTree(page); const fileRow = page.locator('.tree-row', { has: page.locator('.tree-name__label', { hasText: /^a\.txt$/ }) }); // Kebab click opens the menu (no preview/toggle side-effect needed here). await fileRow.click(); // select first await fileRow.locator('.tree-row__kebab').click(); await expect(page.locator('.zddc-menu')).toBeVisible(); await page.keyboard.press('Escape'); await expect(page.locator('.zddc-menu')).toHaveCount(0); // Keyboard menu key (Shift+F10) opens it on the selected row. await fileRow.click(); await page.keyboard.press('Shift+F10'); await expect(page.locator('.zddc-menu')).toBeVisible(); }); });