import { describe, expect, test } from 'bun:test';
import { proxyOverrideScript, rewriteAssetPaths, rewriteCss, rewriteHtml } from './html-rewrite';
const BASE = '/api/dev-proxy/3001';
const attrs = (html: string) => [...html.matchAll(/(?:src|href)="([^"]+)"/g)].map((m) => m[1]!);
// What a browser would resolve the rewritten path to. The bug this file exists for is invisible without
// this step: the rewrite looked right and the browser threw the prefix away.
const resolved = (path: string) => new URL(path, 'https://platform.officer.dev/').pathname;
describe('rewriteHtml', () => {
test('a plain root-absolute asset gets the prefix', () => {
expect(attrs(rewriteHtml('', BASE))).toEqual(['/api/dev-proxy/3001/style.css']);
});
// The blank preview. Bun's dev server emits `href="/../../../chunk-qmw09gd9.css"` — root-absolute AND
// climbing. Prefixed naively the browser normalises it straight back to the platform root and 404s.
test('`..` segments are collapsed BEFORE the prefix, not handed to the browser', () => {
const out = attrs(rewriteHtml('', BASE));
expect(out).toEqual(['/api/dev-proxy/3001/chunk-qmw09gd9.css']);
});
test('and the result survives the browser resolving it — the part that actually failed', () => {
const [href] = attrs(rewriteHtml('