|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { resolve } from 'node:path'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Hosting contract sentinel (Cloudflare Pages). Three of the campaign's |
| 7 | + * production-only defects lived in this layer (a vendor loader stuck behind |
| 8 | + * the index.html -> directory 308, the font catalog shipped without a cache |
| 9 | + * rule, sw.js/HTML that must never be cached). Pin the rules the app relies |
| 10 | + * on so an edit to public/_headers / _redirects / sw.js that drops one turns |
| 11 | + * red here first. The rules themselves are exercised for real by the |
| 12 | + * `e2e-pages` job (wrangler pages dev) and the production smoke. |
| 13 | + */ |
| 14 | +const ROOT = resolve(__dirname, '../..'); |
| 15 | +const read = (rel: string) => readFileSync(resolve(ROOT, rel), 'utf8'); |
| 16 | + |
| 17 | +/** Parse the simple `_headers` format into { path: { header: value } }. */ |
| 18 | +function parseHeaders(src: string): Record<string, Record<string, string>> { |
| 19 | + const out: Record<string, Record<string, string>> = {}; |
| 20 | + let current: string | null = null; |
| 21 | + for (const raw of src.split('\n')) { |
| 22 | + const line = raw.replace(/#.*$/, '').trimEnd(); |
| 23 | + if (!line.trim()) continue; |
| 24 | + if (!/^\s/.test(line)) { |
| 25 | + current = line.trim(); |
| 26 | + out[current] = {}; |
| 27 | + } else if (current) { |
| 28 | + const idx = line.indexOf(':'); |
| 29 | + out[current][line.slice(0, idx).trim().toLowerCase()] = line.slice(idx + 1).trim(); |
| 30 | + } |
| 31 | + } |
| 32 | + return out; |
| 33 | +} |
| 34 | + |
| 35 | +describe('public/_headers', () => { |
| 36 | + const rules = parseHeaders(read('public/_headers')); |
| 37 | + const cc = (path: string) => rules[path]?.['cache-control']; |
| 38 | + |
| 39 | + it('keeps deploy-coupled files uncacheable (a stale one breaks the shell)', () => { |
| 40 | + for (const p of ['/sw.js', '/home.css', '/landing.css', '/lang-switch.js', '/ranui-iife/*']) { |
| 41 | + expect(cc(p), p).toBe('no-cache'); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + it('keeps hashed/immutable assets long-lived: build assets, font catalog, x2t wasm', () => { |
| 46 | + for (const p of ['/assets/*', '/fonts/*', '/sdkjs/common/wasm/x2t/x2t.wasm.gz', '/ran-tokens.*.css']) { |
| 47 | + expect(cc(p), p).toMatch(/max-age=31536000.*immutable/); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it('does not make patched vendor trees immutable (x2t_helper.js and editor HTML must revalidate)', () => { |
| 52 | + for (const p of Object.keys(rules)) { |
| 53 | + if (/^\/(sdkjs|web-apps)\/\*$/.test(p) || p === '/sdkjs/*' || p === '/web-apps/*') { |
| 54 | + expect(cc(p)).not.toMatch(/immutable/); |
| 55 | + } |
| 56 | + } |
| 57 | + expect(Object.keys(rules).some((p) => p.startsWith('/sdkjs/common/wasm/x2t/x2t_helper'))).toBe(false); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('public/_redirects', () => { |
| 62 | + it('canonicalizes the localized landing directory', () => { |
| 63 | + expect(read('public/_redirects')).toMatch(/^\/zh-CN\s+\/zh-CN\/\s+308/m); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('index.html -> directory normalization awareness', () => { |
| 68 | + it('the app never relies on the vendor common loader for PDFs (it breaks behind the 308)', () => { |
| 69 | + const src = read('lib/onlyoffice-editor.ts'); |
| 70 | + expect(src).toMatch(/isForm:\s*false/); |
| 71 | + }); |
| 72 | +}); |
0 commit comments