import type { ModuleGraph } from '../scripts/visual-diff-graph' import type { PageRoute } from '../scripts/visual-diff-plan' import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterAll, describe, expect, it } from 'vitest' import { parse } from 'yaml' import { fileRoutePattern } from '../scripts/console-route-patterns' import { stylesheetSource } from '../scripts/visual-diff-graph' import { changedMessageKeys, changedRouteDefinitions, configImportState, finalPage, fullCaptureReason, pageRoutes, parseRedirects, planSummary, resolveRoute, arrayElements, routeFingerprint, routesUsingKeys, shardCount, unplannedFinalPages, usesKey, visitedPaths, } from '../scripts/visual-diff-plan' function module(hash: string, imports: string[] = [], dynamicImports: string[] = [], code?: string) { return { hash, imports, dynamicImports, ...(code ? { code } : {}) } } function graph(overrides: Partial = {}): ModuleGraph { return { root: '/repo', entries: ['index.html'], modules: { 'index.html': module('html', ['src/main.ts']), 'src/main.ts': module('main', ['\0vue-router/auto-routes', '\0virtual:generated-layouts', 'messages/en.json']), '\0vue-router/auto-routes': module('routes-v1', [], ['src/pages/a.vue', 'src/pages/b.vue']), '\0virtual:generated-layouts': module('layouts-v1', [], ['src/layouts/default.vue', 'src/layouts/naked.vue']), 'messages/en.json': module('messages-v1'), 'src/layouts/default.vue': module('default-layout', ['src/components/Sidebar.vue']), 'src/layouts/naked.vue': module('naked-layout'), 'src/components/Sidebar.vue': module('sidebar', [], [], `t('sidebar-title')`), 'src/pages/a.vue': module('page-a', ['src/components/A.vue', 'src/components/Shared.vue']), 'src/pages/b.vue': module('page-b', ['src/components/B.vue', 'src/components/Shared.vue'], ['src/components/LazyDialog.vue']), 'src/components/A.vue': module('a', [], [], `t("a-title")`), 'src/components/B.vue': module('b', [], [], 't(\'b-title\')'), 'src/components/LazyDialog.vue': module('lazy', [], [], 't(`dialog-title`)'), 'src/components/Shared.vue': module('shared'), ...overrides, } as ModuleGraph['modules'], } } const routeA: PageRoute = { page: 'src/pages/a.vue', pattern: '^/a/?$', params: 0, layouts: ['src/layouts/default.vue'] } const routeB: PageRoute = { page: 'src/pages/b.vue', pattern: '^/b/?$', params: 0, layouts: ['src/layouts/default.vue'] } const routeLogin: PageRoute = { page: 'src/pages/a.vue', pattern: '^/login/?$', params: 0, layouts: ['src/layouts/naked.vue'] } describe('routeFingerprint', () => { const base = graph() it('ignores changes in other pages, including their lazy imports', () => { const head = graph({ 'src/components/B.vue': module('b-changed'), 'src/components/LazyDialog.vue': module('lazy-changed'), }) expect(routeFingerprint(head, routeA)).toBe(routeFingerprint(base, routeA)) expect(routeFingerprint(head, routeB)).not.toBe(routeFingerprint(base, routeB)) }) it('flags every page that imports a changed shared component', () => { const head = graph({ 'src/components/Shared.vue': module('shared-changed') }) expect(routeFingerprint(head, routeA)).not.toBe(routeFingerprint(base, routeA)) expect(routeFingerprint(head, routeB)).not.toBe(routeFingerprint(base, routeB)) }) it('flags only the routes rendered in a changed layout', () => { const head = graph({ 'src/components/Sidebar.vue': module('sidebar-changed') }) expect(routeFingerprint(head, routeA)).not.toBe(routeFingerprint(base, routeA)) expect(routeFingerprint(head, routeLogin)).toBe(routeFingerprint(base, routeLogin)) }) it('flags every route when the app entry changes', () => { const head = graph({ 'src/main.ts': module('main-changed', ['\0vue-router/auto-routes', '\0virtual:generated-layouts', 'messages/en.json']) }) expect(routeFingerprint(head, routeLogin)).not.toBe(routeFingerprint(base, routeLogin)) }) it('does not flag existing routes when a page is added or a message changes', () => { const head = graph({ '\0vue-router/auto-routes': module('routes-v2', [], ['src/pages/a.vue', 'src/pages/b.vue', 'src/pages/c.vue']), 'src/pages/c.vue': module('page-c'), 'messages/en.json': module('messages-v2'), }) expect(routeFingerprint(head, routeA)).toBe(routeFingerprint(base, routeA)) }) it('flags a route that now resolves to another page or layout', () => { expect(routeFingerprint(base, { ...routeA, page: 'src/pages/b.vue' })).not.toBe(routeFingerprint(base, routeA)) expect(routeFingerprint(base, { ...routeA, layouts: ['src/layouts/naked.vue'] })).not.toBe(routeFingerprint(base, routeA)) }) }) describe('message keys', () => { it('reports changed and removed keys, and added ones separately', () => { expect(changedMessageKeys( { 'a-title': 'A', 'b-title': 'B', 'nested': { kept: 'x', gone: 'y' } }, { 'a-title': 'A2', 'b-title': 'B', 'nested': { kept: 'x' }, 'new-key': 'N' }, )).toEqual({ changed: ['a-title', 'nested.gone'], added: ['new-key'] }) }) it('maps a key to the routes whose code uses it, on either revision', () => { const base = graph() const head = graph({ 'src/components/A.vue': module('a') }) const modules = (g: ModuleGraph) => new Map([ ['a', new Set(Object.keys(g.modules).filter(key => key !== 'src/pages/b.vue' && !key.includes('/B.vue') && !key.includes('LazyDialog')))], ['b', new Set(['src/pages/b.vue', 'src/components/B.vue', 'src/components/LazyDialog.vue'])], ]) const sides = [{ graph: base, modules: modules(base) }, { graph: head, modules: modules(head) }] expect([...routesUsingKeys(['a-title'], sides)!]).toEqual(['a']) expect([...routesUsingKeys(['dialog-title'], sides)!]).toEqual(['b']) expect([...routesUsingKeys(['sidebar-title'], sides)!]).toEqual(['a']) }) it('finds keys built from a static prefix', () => { expect(usesKey('t(`plans-${name}`)', 'plans-pro')).toBe(true) expect(usesKey(`t('plans-' + name)`, 'plans-pro')).toBe(true) expect(usesKey('t(`${name}`)', 'plans-pro')).toBe(false) expect(usesKey('t(`other-${name}`)', 'plans-pro')).toBe(false) }) it('keeps computed consumers when another route uses the key literally', () => { const base = graph({ 'src/components/B.vue': module('b', [], [], 't(`a-${suffix}`)') }) const modules = new Map([ ['a', new Set(['src/pages/a.vue', 'src/components/A.vue'])], ['b', new Set(['src/pages/b.vue', 'src/components/B.vue'])], ]) expect([...routesUsingKeys(['a-title'], [{ graph: base, modules }])!].sort()).toEqual(['a', 'b']) }) it('flags routes that already looked up an added key, and ignores added keys nothing uses', () => { const base = graph() const modules = new Map([['a', new Set(['src/pages/a.vue', 'src/components/A.vue'])], ['b', new Set(['src/pages/b.vue', 'src/components/B.vue'])]]) expect([...routesUsingKeys(['b-title', 'unused-new-key'], [{ graph: base, modules }], new Set(['b-title', 'unused-new-key']))!]).toEqual(['b']) }) it('gives up when a key is never written literally (computed lookups)', () => { const base = graph() expect(routesUsingKeys(['plans.pro'], [{ graph: base, modules: new Map([['a', new Set(Object.keys(base.modules))]]) }])).toBeNull() }) }) describe('changedRouteDefinitions', () => { const source = (helper: string) => `async function helper(page) {\n ${helper}\n}\n\nasync function other(page) {\n await helper(page)\n}\n\nexport const visualDiffViewport = { width: 1440, height: 900 }\n\nexport const visualDiffRoutes = []\n` const routes = [ { slug: 'plain', path: '/a', definition: JSON.stringify({ path: '/a', prepare: null }) }, { slug: 'direct', path: '/b', definition: JSON.stringify({ path: '/b', prepare: 'page => helper(page)' }) }, { slug: 'indirect', path: '/c', definition: JSON.stringify({ path: '/c', prepare: 'page => other(page)' }) }, ] it('flags routes whose steps or helpers changed, transitively', () => { expect(changedRouteDefinitions({ source: source('await page.click()'), routes }, { source: source('await page.hover()'), routes })).toEqual(['direct', 'indirect']) }) it('flags new and edited routes only', () => { const head = [...routes.slice(0, 2), { ...routes[2], definition: JSON.stringify({ path: '/c2' }) }, { slug: 'new', path: '/d', definition: '{}' }] expect(changedRouteDefinitions({ source: source('x'), routes }, { source: source('x'), routes: head })).toEqual(['indirect', 'new']) }) it('flags every route when the viewport changes', () => { const head = source('x').replace('1440', '1280') expect(changedRouteDefinitions({ source: source('x'), routes }, { source: head, routes })).toEqual(['plain', 'direct', 'indirect']) }) }) describe('route list sources', () => { const config = (fixtures: string, extra = '') => `async function helper(page) {\n await page.goto('/app/com.demo.app/getting-started')\n}\n\nexport const visualDiffRoutes = [\n { slug: 'plain', path: '/a' },\n ...[\n ${fixtures}\n ].map(({ slug, progress }) => ({ slug, path: '/apps', prepare: page => helper(page, progress) })),\n ${extra}\n]\n` const routes = ['plain', 'mapped-a', 'mapped-b'].map(slug => ({ slug, path: slug === 'plain' ? '/a' : '/apps', definition: JSON.stringify({ path: '/apps', prepare: 'page => helper(page, progress)' }) })) const fixtures = `{ slug: 'mapped-a', progress: 'none' },\n { slug: 'mapped-b', progress: 'ota' },` it('flags the routes of a mapped array when only a captured fixture value changes', () => { const head = fixtures.replace(`'ota'`, `'builder'`) expect(changedRouteDefinitions({ source: config(fixtures), routes }, { source: config(head), routes })).toEqual(['mapped-a', 'mapped-b']) }) it('gives up on a changed list element that declares no slug', () => { expect(changedRouteDefinitions({ source: config(fixtures), routes }, { source: config(fixtures, '...extraRoutes,'), routes })).toBeNull() }) it('captures everything when a config import, named or not, changed through any module it loads', () => { const withImport = (line: string) => `${line}\n\n${config(fixtures)}` const source = withImport(`import { STEP_IDS } from './support/steps'`) const usesSteps = routes.map(route => route.slug === 'plain' ? { ...route, definition: JSON.stringify({ path: '/a', prepare: 'page => STEP_IDS' }) } : route) const files = (effect: string) => ({ 'playwright/support/steps.ts': `import '../../src/effect'\nexport const STEP_IDS = ['a']\n`, 'src/effect.ts': effect, } as Record) const graphs = { base: graph(), head: graph() } const state = (baseEffect: string, headEffect: string) => (specifier: string) => configImportState(specifier, graphs, (side, path) => files(side === 'base' ? baseEffect : headEffect)[path]) // The real traversal result, through the planner: a side effect behind a named import. expect(changedRouteDefinitions({ source, routes: usesSteps }, { source, routes: usesSteps }, state(`process.env.X = '1'`, `process.env.X = '2'`))).toBeNull() expect(changedRouteDefinitions({ source, routes: usesSteps }, { source, routes: usesSteps }, state(`process.env.X = '1'`, `process.env.X = '1'`))).toEqual([]) // An edited import line captures everything too. expect(changedRouteDefinitions({ source, routes }, { source: withImport(`import { STEP_IDS } from './support/other'`), routes })).toBeNull() }) it('treats side-effect imports as imports: edited or changed, they capture everything', () => { const source = `import '../src/setup'\n\n${config(fixtures)}` expect(changedRouteDefinitions({ source: config(fixtures), routes }, { source, routes })).toBeNull() expect(changedRouteDefinitions({ source, routes }, { source, routes }, specifier => specifier === '../src/setup' ? 'changed' : 'unchanged')).toBeNull() expect(changedRouteDefinitions({ source, routes }, { source, routes }, () => 'unchanged')).toEqual([]) expect(changedRouteDefinitions({ source, routes }, { source, routes }, () => 'unknown')).toBeNull() }) it('follows a config import through bundled and unbundled modules', () => { const base = graph({ 'src/services/steps.ts': module('steps', ['src/services/ids.ts']), 'src/services/ids.ts': module('ids') }) const head = graph({ 'src/services/steps.ts': module('steps', ['src/services/ids.ts']), 'src/services/ids.ts': module('ids-changed') }) const files = (overrides: Record = {}) => ({ 'playwright/support/query.ts': `import { setup } from '../../src/setup'\nexport const query = 1\n`, 'src/setup.ts': `import './effect'\nimport { STEPS } from './services/steps'\nexport const setup = STEPS\n`, 'src/effect.ts': `process.env.X = '1'\n`, ...overrides, } as Record) const read = (baseFiles: Record, headFiles: Record) => (side: 'base' | 'head', path: string) => (side === 'base' ? baseFiles : headFiles)[path] const same = read(files(), files()) expect(configImportState('./support/query', { base, head: base }, same)).toBe('unchanged') // A bundled module two imports away changed. expect(configImportState('./support/query', { base, head }, same)).toBe('changed') // An unbundled file two imports away changed (a side effect only). expect(configImportState('./support/query', { base, head: base }, read(files(), files({ 'src/effect.ts': `process.env.X = '2'\n` })))).toBe('changed') // A file that cannot be found at all leaves the effect unknown. expect(configImportState('./support/missing', { base, head: base }, same)).toBe('unknown') expect(configImportState('@playwright/test', { base, head: base }, same)).toBe('unchanged') }) it('compares fixture strings verbatim, whitespace included', () => { const head = fixtures.replace(`'ota'`, `'ota '`) expect(changedRouteDefinitions({ source: config(fixtures), routes }, { source: config(head), routes })).toEqual(['mapped-a', 'mapped-b']) }) it('follows page.goto() through helpers, and gives up on computed targets', () => { const [, mapped] = routes expect(visitedPaths(mapped, config(fixtures))).toEqual(['/apps', '/app/com.demo.app/getting-started']) expect(visitedPaths(mapped, config(fixtures).replace(`'/app/com.demo.app/getting-started'`, '`/app/${appId}`'))).toBeNull() }) it('can attribute every route of the real config', () => { const source = readFileSync(join(import.meta.dirname, '../playwright/visual-diff.config.ts'), 'utf8') const elements = arrayElements(source.slice(source.indexOf('export const visualDiffRoutes'))) expect(elements.length).toBeGreaterThan(0) // An element without a slug literal would make every config edit capture all routes. expect(elements.filter(element => !/\bslug:/.test(element))).toEqual([]) for (const match of source.matchAll(/\.goto\(\s*([^,)]*)/g)) expect(match[1].trim(), 'a computed page.goto() target makes its route always captured').toMatch(/^(['"`])[^'"`$]*\1$/) }) }) describe('stylesheetSource', () => { it('includes the versions of the packages a stylesheet loads', () => { const root = mkdtempSync(join(tmpdir(), 'visual-diff-css-')) try { writeFileSync(join(root, 'package.json'), '{}') mkdirSync(join(root, 'node_modules/daisyui'), { recursive: true }) writeFileSync(join(root, 'node_modules/daisyui/package.json'), '{"name":"daisyui","version":"5.1.0"}') writeFileSync(join(root, 'style.css'), `@import 'tailwindcss' source(none);\n@plugin "daisyui/index.js" {}\n@import './local.css';\n`) const source = stylesheetSource(root, join(root, 'style.css')) expect(source).toContain('/* daisyui@5.1.0 */') expect(source).toContain('/* tailwindcss@missing */') expect(source).not.toContain('local.css@') } finally { rmSync(root, { recursive: true, force: true }) } }) }) describe('fullCaptureReason', () => { it.each([ ['public/fonts/inter.woff2', true], ['supabase/migrations/20260101_x.sql', true], ['supabase/functions/_backend/private/stats.ts', true], ['supabase/seed.sql', true], ['scripts/visual-diff.ts', true], ['playwright/support/consoleQuery.ts', true], ['.github/workflows/visual-diff.yml', true], ['supabase/tests/26_test.sql', false], ['supabase/functions/updates/index.ts', false], ['supabase/functions/_backend/emails/welcome.ts', false], ['playwright/e2e/apps.spec.ts', false], ['playwright/visual-diff.config.ts', false], ['src/pages/apps.vue', false], ['tests/foo.test.ts', false], ['supabase/migration_guide.md', false], ])('%s -> %s', (file, full) => { expect(Boolean(fullCaptureReason(file))).toBe(full) }) }) describe('pageRoutes and resolveRoute', () => { const root = mkdtempSync(join(tmpdir(), 'visual-diff-plan-')) afterAll(() => rmSync(root, { recursive: true, force: true })) const pages = { 'src/pages/app/[app].channels.vue': '