openmaic-custom/tests/export/inline-assets-importmap.test.ts
wyuc 86c8e0c543
feat: inline external assets at export for offline classrooms (#596) (#613)
* feat(export): add external-asset reference scanner for HTML inlining

* feat(export): add cached asset fetcher and data-uri encoder

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(export): recursively inline url() refs inside stylesheets

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(export): orchestrate inlining of links, scripts, images, and inline css

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(export): drop dead escapeAttr, harden importmap skip in scanner

* feat(export): inline importmap modules incl three/addons graph closure

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(export): extract shared module to break inline-assets import cycle

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(export): inline interactive-scene assets into .maic.zip at export

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(export): inline interactive assets in Resource Pack export too

* docs: document offline export asset inlining

* style: prettier formatting

* fix(test): resolve tsc type errors in export-classroom-inline test

* fix(export): keep importmap prefix fallback on partial failure, chunk base64, preserve media attr, log pptx inline failures

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* perf(export): parallelize asset fetching, share fetcher across scenes, promise-cache

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(export): retry transient asset fetches, bound concurrency, warn user on partial inline

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* perf(export): inline only woff2 fonts, drop woff/ttf siblings to halve size

Within @font-face blocks that have a woff2 url(), rewrite sibling woff/ttf/otf/eot
refs to url(about:invalid) instead of inlining them. Falls back to full inline for
ttf-only blocks. Drops katex output from ~1.79MB to ~0.71MB (~3x fewer font fetches).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* i18n(export): translate partial-inline warning and surface failed asset hosts

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(proxy-media): follow redirects with per-hop SSRF revalidation (#398)

Replace blanket redirect rejection with a bounded 5-hop follow loop that
re-validates every Location target through validateUrlForSSRF before
fetching, preventing redirect-to-internal SSRF while allowing CDN
redirects (e.g. cdn.tailwindcss.com 302→/3.4.17).

* feat(export): route asset inlining through proxy-media to bypass CORS

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(export): keep importmap prefix fallback, forward proxy 4xx, cap proxy size, re-export FetchAsset

* style: prettier formatting for proxy-media + proxied-fetch

* fix(export): report CSS-nested asset failures in inline report

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: 杨慎 <117187635+cosarah@users.noreply.github.com>
2026-05-31 18:36:39 +08:00

92 lines
3.9 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { buildInlinedImportmap } from '@/lib/export/inline-assets-importmap';
const enc = (s: string) => ({ bytes: new TextEncoder().encode(s), contentType: 'text/javascript' });
describe('buildInlinedImportmap', () => {
it('inlines a direct module entry to a data: URI', async () => {
const fetchAsset = async (url: string) =>
url === 'https://unpkg.com/three@0.160.0/build/three.module.js'
? enc('export const THREE=1')
: null;
const { imports, report } = await buildInlinedImportmap(
{ three: 'https://unpkg.com/three@0.160.0/build/three.module.js' },
["import * as THREE from 'three';"],
fetchAsset,
);
expect(imports.three).toMatch(/^data:text\/javascript;base64,/);
expect(report.inlined).toContain('https://unpkg.com/three@0.160.0/build/three.module.js');
});
it('expands a prefix entry (three/addons/) into explicit full-specifier data: entries and drops the prefix', async () => {
const base = 'https://unpkg.com/three@0.160.0/examples/jsm/';
const fetchAsset = async (url: string) => {
if (url === 'https://unpkg.com/three@0.160.0/build/three.module.js')
return enc('export const THREE=1');
if (url === base + 'controls/OrbitControls.js')
return enc("import * as THREE from 'three'; export class OrbitControls{}");
return null;
};
const moduleBodies = [
"import * as THREE from 'three'; import { OrbitControls } from 'three/addons/controls/OrbitControls.js';",
];
const { imports } = await buildInlinedImportmap(
{ three: 'https://unpkg.com/three@0.160.0/build/three.module.js', 'three/addons/': base },
moduleBodies,
fetchAsset,
);
expect(imports['three']).toMatch(/^data:/);
expect(imports['three/addons/controls/OrbitControls.js']).toMatch(/^data:/);
expect(imports['three/addons/']).toBeUndefined();
});
it('recursively resolves nested addon imports', async () => {
const base = 'https://unpkg.com/three@0.160.0/examples/jsm/';
const fetchAsset = async (url: string) => {
if (url === 'https://x/three.js') return enc('export const THREE=1');
if (url === base + 'a.js') return enc("import 'three/addons/b.js'; export const a=1;");
if (url === base + 'b.js') return enc("import * as THREE from 'three'; export const b=2;");
return null;
};
const { imports } = await buildInlinedImportmap(
{ three: 'https://x/three.js', 'three/addons/': base },
["import 'three/addons/a.js';"],
fetchAsset,
);
expect(imports['three/addons/a.js']).toMatch(/^data:/);
expect(imports['three/addons/b.js']).toMatch(/^data:/);
});
it('records failures for unfetchable modules and leaves their specifier unmapped', async () => {
const { imports, report } = await buildInlinedImportmap(
{ three: 'https://dead/three.js' },
["import 'three';"],
async () => null,
);
expect(imports.three).toBeUndefined();
expect(report.failed.map((f) => f.url)).toContain('https://dead/three.js');
});
it('ignores relative/bare specifiers not present in the importmap', async () => {
const { imports } = await buildInlinedImportmap(
{ three: 'https://x/three.js' },
["import './local.js'; import 'react';"],
async (url: string) => (url === 'https://x/three.js' ? enc('export const T=1') : null),
);
// only 'three' was imported among mapped specifiers? Actually 'three' is NOT imported here.
// local.js and react are not in the importmap → no entries produced.
expect(Object.keys(imports)).toEqual([]);
});
it('handles dynamic import() specifiers', async () => {
const fetchAsset = async (url: string) =>
url === 'https://x/three.js' ? enc('export const T=1') : null;
const { imports } = await buildInlinedImportmap(
{ three: 'https://x/three.js' },
["const m = await import('three');"],
fetchAsset,
);
expect(imports.three).toMatch(/^data:/);
});
});