Files
rose-ash/tests/playwright/navigation.spec.js
giles b274e428eb WASM kernel fixes: parse, env sync, iterative CEK, click delegation
Browser kernel:
- Add `parse` native fn (matches server: unwrap single, list for multiple)
- Restore env==global_env guard on _env_bind_hook (let bindings must not
  leak to _vm_globals — caused JIT CSSX "Not callable: nil" errors)
- Add _env_bind_hook call in env_set_id so set! mutations sync to VM globals
- Fire _vm_global_set_hook from OP_DEFINE so VM defines sync back to CEK env

CEK evaluator:
- Replace recursive cek_run with iterative while loop using sx_truthy
  (previous attempt used strict Bool true matching, broke in wasm_of_ocaml)
- Remove dead cek_run_iterative function

Web modules:
- Remove find-matching-route and parse-route-pattern stubs from
  boot-helpers.sx that shadowed real implementations from router.sx
- Sync boot-helpers.sx to dist/static dirs for bytecode compilation

Platform (sx-platform.js):
- Set data-sx-ready attribute after boot completes (was only in boot-init
  which sx-platform.js doesn't call — it steps through boot manually)
- Add document-level click delegation for a[sx-get] links as workaround
  for bytecoded bind-event not attaching per-element listeners (VM closure
  issue under investigation — bind-event runs but dom-add-listener calls
  don't result in addEventListener)

Tests:
- New test_kernel.js: 24 tests covering env sync, parse, route matching,
  host FFI/preventDefault, deep recursion
- New navigation test: "sx-get link fetches SX not HTML and preserves layout"
  (currently catches layout breakage after SPA swap — known issue)

Known remaining issues:
- JIT CSSX failures: closure-captured variables resolve to nil in VM bytecode
- SPA content swap via execute-request breaks page layout
- Bytecoded bind-event doesn't attach per-element addEventListener (root
  cause unknown — when listen-target guard appears to block despite element
  being valid)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 13:33:13 +00:00

203 lines
7.0 KiB
JavaScript

// Navigation tests for sx-docs
// Verifies navigation works correctly with the OCaml sx-host.
const { test, expect } = require('playwright/test');
const { BASE_URL, waitForSxReady, loadPage } = require('./helpers');
test.describe('Page Navigation', () => {
test('clicking nav button navigates to new page', async ({ page }) => {
const errors = [];
page.on('console', msg => {
if (msg.type() === 'error') errors.push(msg.text());
});
await loadPage(page, '(geography)');
// Click "Reactive Islands" nav link
await page.click('a[href*="geography.(reactive)"]:not([href*="runtime"])');
await expect(page).toHaveURL(/reactive/, { timeout: 5000 });
// Page should show Reactive Islands content
const body = await page.textContent('body');
expect(body).toContain('Reactive Islands');
// No SX evaluation errors
const sxErrors = errors.filter(e => e.includes('Undefined symbol'));
expect(sxErrors).toHaveLength(0);
});
test('clicking header logo navigates home', async ({ page }) => {
const errors = [];
page.on('console', msg => {
if (msg.type() === 'error') errors.push(msg.text());
});
await loadPage(page, '(geography)');
// Click the logo in the header island
await page.click('[data-sx-island="layouts/header"] a[href="/sx/"]');
await expect(page).toHaveURL(/\/sx\/?$/, { timeout: 5000 });
// No SX evaluation errors
const sxErrors = errors.filter(e => e.includes('Undefined symbol'));
expect(sxErrors).toHaveLength(0);
});
test('back button works after navigation', async ({ page }) => {
const errors = [];
page.on('console', msg => {
if (msg.type() === 'error') errors.push(msg.text());
});
await loadPage(page, '(geography)');
// Navigate to Reactive Islands
await page.click('a[href*="geography.(reactive)"]:not([href*="runtime"])');
await expect(page).toHaveURL(/reactive/, { timeout: 5000 });
// Go back
await page.goBack();
await expect(page).toHaveURL(/geography/, { timeout: 5000 });
await expect(page).not.toHaveURL(/reactive/);
// Geography heading should be visible
const heading = await page.locator('h1, h2').first();
await expect(heading).toContainText('Geography', { timeout: 5000 });
// No SX errors
const sxErrors = errors.filter(e => e.includes('Undefined symbol'));
expect(sxErrors).toHaveLength(0);
});
test('no console errors on page load', async ({ page }) => {
const errors = [];
page.on('console', msg => {
if (msg.type() === 'error' && !msg.text().includes('404'))
errors.push(msg.text());
});
await loadPage(page, '(geography)');
// No JIT or SX errors
const sxErrors = errors.filter(e =>
e.includes('Undefined symbol') || e.includes('Not callable'));
expect(sxErrors).toHaveLength(0);
});
test('copyright shows current route after SX navigation', async ({ page }) => {
await loadPage(page, '');
// Mark the page to verify SX navigation (not full reload)
await page.evaluate(() => window.__sx_nav_marker = true);
// Before: copyright shows the current path
const before = await page.evaluate(() =>
document.querySelector('[data-sx-lake="copyright"]')?.textContent);
expect(before).toContain('/sx/');
// Navigate via SX (sx-get link)
await page.click('a[sx-get*="(geography)"]');
await expect(page).toHaveURL(/geography/, { timeout: 5000 });
// Verify SX navigation (marker survives SX nav, lost on reload)
const marker = await page.evaluate(() => window.__sx_nav_marker);
expect(marker).toBe(true);
// After: copyright must still show a route path
const after = await page.evaluate(() =>
document.querySelector('[data-sx-lake="copyright"]')?.textContent);
expect(after).toContain('geography');
});
test('stepper persists index across navigation', async ({ page }) => {
await loadPage(page, '');
// Get the initial stepper index
const getIndex = () => page.evaluate(() => {
const el = document.querySelector('[data-sx-island="home/stepper"]');
const m = el && el.textContent.match(/(\d+)\s*\/\s*\d+/);
return m ? parseInt(m[1]) : null;
});
const initial = await getIndex();
expect(initial).not.toBeNull();
// Advance the stepper
await page.evaluate(() => {
const btns = document.querySelectorAll('[data-sx-island="home/stepper"] button');
if (btns.length >= 2) btns[1].click(); // next button
});
await page.waitForTimeout(300);
const advanced = await getIndex();
expect(advanced).toBe(initial + 1);
// Navigate away
await page.click('a[sx-get*="(geography)"]');
await expect(page).toHaveURL(/geography/, { timeout: 5000 });
// Navigate back home
await page.evaluate(() => {
const link = document.querySelector('a[sx-get*="/sx/"]');
if (link) link.click();
});
await expect(page).toHaveURL(/\/sx\/?$/, { timeout: 5000 });
// Stepper should still show the advanced index
const afterNav = await getIndex();
expect(afterNav).toBe(advanced);
});
test('sx-get link fetches SX not HTML and preserves layout', async ({ page }) => {
await loadPage(page, '');
// Mark page so we can detect full reload vs SPA nav
await page.evaluate(() => window.__spa_marker = true);
// Click a nav link (Geography)
await page.click('a[sx-get*="(geography)"]');
await expect(page).toHaveURL(/geography/, { timeout: 5000 });
// Must be SPA navigation — marker survives (full reload clears it)
const marker = await page.evaluate(() => window.__spa_marker);
expect(marker).toBe(true);
// After navigation, #sx-nav and #main-content should still be
// vertically stacked (not side-by-side). Check that nav is above content.
const layout = await page.evaluate(() => {
const nav = document.querySelector('#sx-nav');
const main = document.querySelector('#main-content, #main-panel');
if (!nav || !main) return { error: 'missing elements', nav: !!nav, main: !!main };
const navRect = nav.getBoundingClientRect();
const mainRect = main.getBoundingClientRect();
return {
navBottom: navRect.bottom,
mainTop: mainRect.top,
navRight: navRect.right,
mainLeft: mainRect.left,
// Nav should end before main starts (vertically stacked)
verticallyStacked: navRect.bottom <= mainRect.top + 5,
// Nav and main should overlap horizontally (not side-by-side)
horizontalOverlap: navRect.left < mainRect.right && mainRect.left < navRect.right
};
});
expect(layout.verticallyStacked).toBe(true);
expect(layout.horizontalOverlap).toBe(true);
});
test('header island renders with SSR', async ({ page }) => {
await loadPage(page, '(geography)');
// Header should be visible
const header = page.locator('[data-sx-island="layouts/header"]');
await expect(header).toBeVisible();
// Should contain the logo
await expect(header).toContainText('sx');
// Should contain copyright
await expect(header).toContainText('Giles Bradshaw');
});
});