import hashlib import json import posixpath import re from pathlib import Path APP = Path('/app') ROOT = APP / 'vendor/vite' NODE = ROOT / 'packages/vite/src/node' MUTABLE = { 'packages/vite/src/node/constants.ts', 'packages/vite/src/node/plugins/html.ts', 'packages/vite/src/node/server/bundledDev.ts', } CASES = json.loads((Path('/tests/fixtures/cases.json')).read_text()) HASHES = json.loads((Path('/tests/fixtures/source_hashes.json')).read_text()) def read(relative): return (NODE / relative).read_text() def filename_from_constants(source): matches = re.findall( r"export\s+const\s+BUNDLED_DEV_CLIENT_FILENAME(?:\s*:\s*string)?\s*=\s*(['\"])([^'\"]+)\1", source, ) assert len(matches) == 1 return matches[0][1] def html_implementation(source): join = re.search( r"path\.posix\.join\(\s*config\.base\s*,\s*BUNDLED_DEV_CLIENT_FILENAME\s*,?\s*\)", source, ) assert join is not None window = source[max(0, join.start() - 1000):join.end() + 1000] assert "tag: 'script'" in window assert "type: 'module'" in window assert re.search(r'injectToHead\([\s\S]*?\[[\s\S]*?\][\s\S]*?,\s*true\s*,?\s*\)', window) assert "config.command === 'serve'" in window assert "this.environment.config.consumer === 'client'" in window assert 'this.environment.config.isBundled' in window injection_index = source.find('BUNDLED_DEV_CLIENT_FILENAME', join.start() - 200) asset_index = source.find('result = injectToHead(result, assetTags)') assert injection_index > asset_index >= 0 return lambda command, consumer, bundled, base, application_src: ( [posixpath.join(base, filename_from_constants(read('constants.ts'))), application_src] if command == 'serve' and consumer == 'client' and bundled else [application_src] ) def store_output_behavior(source, chunk_source, is_entry): method = re.search( r"private\s+storeOutputFiles\([^)]*\)\s*:\s*void\s*\{(?P[\s\S]*?)\n \}\n\n private\s+async\s+getRolldownOptions", source, ) assert method is not None body = method.group('body') assert 'this.memoryFiles.set(BUNDLED_DEV_CLIENT_FILENAME' in body assert 'outputFile.isEntry' not in body assert 'runtimePath' not in body assert re.search(r"source:\s*this\.viteRuntime", body) return chunk_source def test_gate_frozen_source_tree_and_report_contract(): for relative, expected in HASHES.items(): path = ROOT / relative assert path.is_file(), relative if relative not in MUTABLE: assert hashlib.sha256(path.read_bytes()).hexdigest() == expected, relative assert 'export const CLIENT_PUBLIC_PATH' in read('constants.ts') assert 'export function buildHtmlPlugin' in read('plugins/html.ts') assert 'export class BundledDev' in read('server/bundledDev.ts') report = json.loads((APP / 'regression-report.json').read_text()) assert report['output_schema_version'] == CASES['output_schema_version'] assert report['status'] == 'pass' assert set(report['checks']) == { 'shared_filename', 'html_injection', 'mode_gating', 'entry_chunk_passthrough' } assert all(report['checks'].values()) def test_core_shared_filename_is_canonical_across_modules(): constants = read('constants.ts') html = read('plugins/html.ts') server = read('server/bundledDev.ts') filename = filename_from_constants(constants) assert filename == 'bundledDevClient.mjs' entry = re.search(r'BUNDLED_DEV_CLIENT_ENTRY[\s\S]{0,260}', constants) assert entry is not None assert 'dist/client' in entry.group(0) assert 'BUNDLED_DEV_CLIENT_FILENAME' in entry.group(0) assert 'dist/client/bundledDevClient.mjs' not in entry.group(0) assert re.search(r"import\s*\{[^}]*BUNDLED_DEV_CLIENT_FILENAME[^}]*\}\s*from\s*'\.\./constants'", html) assert re.search(r"import\s*\{[^}]*BUNDLED_DEV_CLIENT_FILENAME[^}]*\}\s*from\s*'\.\./constants'", server) assert "const BUNDLED_DEV_CLIENT_FILENAME =" not in server def test_core_html_ordering_and_base_paths(): behavior = html_implementation(read('plugins/html.ts')) for case in CASES['html_cases']: scripts = behavior('serve', 'client', True, case['base'], case['application_src']) assert scripts[0] == posixpath.join(case['base'], 'bundledDevClient.mjs') assert scripts[1] == case['application_src'] assert scripts.count(scripts[0]) == 1 def test_core_entry_chunks_are_served_without_runtime_rewrite(): source = CASES['entry_chunk'] served_entry = store_output_behavior(read('server/bundledDev.ts'), source, True) served_non_entry = store_output_behavior(read('server/bundledDev.ts'), source, False) assert served_entry == source assert served_non_entry == source assert 'bundledDevClient.mjs' not in served_entry def test_edge_non_target_modes_remain_unchanged(): behavior = html_implementation(read('plugins/html.ts')) application_src = '/assets/edge.js' for mode in CASES['unchanged_modes']: scripts = behavior( mode['command'], mode['consumer'], mode['is_bundled'], '/edge/', application_src ) assert scripts == [application_src] def test_edge_no_hardcoded_root_url_or_duplicate_runtime_owner(): html = read('plugins/html.ts') server = read('server/bundledDev.ts') assert "src: '/bundledDevClient.mjs'" not in html assert 'source = `import ${JSON.stringify(runtimePath)}' not in server assert "import path from 'node:path'" not in server