Files
tehran_taj/scripts/test-image-fallbacks.cjs
T
mahmoodandClaude Code 3e9f532c6c fix(images): ship Linux codecs and preserve original-image fallback
Vendor the locked sharp Linux runtime and verify it during Docker builds. Add complete image response lengths and persistent frontend fallbacks, with real-codec HTTP and isolated release regression tests. Refresh tested API, admin and customer release artifacts.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-26 18:12:21 +03:30

55 lines
3.1 KiB
JavaScript

// Lightweight regression tests using the repo's existing compiler, not a new test framework.
const assert = require('node:assert/strict');
const path = require('node:path');
const { createRequire } = require('node:module');
const root = path.resolve(__dirname, '..');
const esbuild = createRequire(path.join(root, 'apps/api/package.json'))('esbuild');
let scenarios = 0;
for (const app of ['user-app-v2', 'admin-panel']) {
for (const base of app === 'admin-panel' ? ['/api/', '/backend/', 'https://api.example.test/api/'] : ['/api/']) {
const result = esbuild.buildSync({
stdin: {
contents: "export * from './src/utils/image'; export { computed } from 'vue';",
resolveDir: path.join(root, 'apps', app), loader: 'ts',
},
bundle: true, write: false, platform: 'node', format: 'cjs',
define: { 'import.meta.env.VITE_API_BASE_URL': JSON.stringify(base) },
});
const mod = { exports: {} };
new Function('module', 'exports', 'require', result.outputFiles[0].text)(mod, mod.exports, require);
const { imageUrl, originalImageUrl, useImageFallback, computed } = mod.exports;
const optimized = imageUrl('/uploads/sample.jpg', 1600);
const { imageSource, imageFailed } = useImageFallback();
assert.equal(optimized, `${base}img/sample.jpg?w=1600`);
const rendered = computed(() => imageSource(optimized));
assert.equal(rendered.value, optimized);
imageFailed(optimized);
assert.equal(rendered.value, '/uploads/sample.jpg', 'Vue must invalidate the rendered source');
for (let i = 0; i < 5; i++) {
imageFailed(optimized); // Original fails too: never restore the bad resize.
imageFailed(imageSource(optimized));
assert.equal(imageSource(optimized), '/uploads/sample.jpg');
}
const next = imageUrl('/uploads/next.webp', 400);
assert.equal(imageSource(next), next, 'A different image must not inherit failure');
for (const untouched of [undefined, '/uploads/sample.jpg', 'blob:test', 'data:image/png;base64,AA==', '/assets/logo.svg', 'https://unrelated.example.test/pic.jpg', 'https://unrelated.example.test/api/img/pic.jpg?w=400']) {
imageFailed(untouched);
assert.equal(imageSource(untouched), untouched);
}
for (const original of ['/uploads/photo%20one.jpg', '/uploads/photo.jpg?v=1', '/uploads/photo.jpg#crop', '/api/uploads/photo%20one.jpg', '/api/uploads/photo.jpg?v=1']) {
const source = imageUrl(original, 400);
assert.equal(source, original, 'Do not double-encode or rewrite an original URL');
imageFailed(source);
assert.equal(imageSource(source), original);
}
for (const invalid of ['../a.jpg', '%2Fetc%2Fpasswd', '%5Ca.jpg', '%00.jpg', '%ZZ', '']) {
assert.equal(originalImageUrl(`${base}img/${invalid}?w=400`), undefined);
}
assert.equal(originalImageUrl(`${base}img/photo%20one.jpg?w=400`), '/uploads/photo%20one.jpg');
assert.equal(useImageFallback().imageSource(optimized), optimized, 'Independent component state');
scenarios++;
console.log(`PASS ${app}, API base ${base}: persistent fallback, no retry loop, safe URLs`);
}
}
console.log(`Image fallback regression checks passed (${scenarios} configurations).`);