mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 12:48:35 +00:00
579 lines
30 KiB
JavaScript
579 lines
30 KiB
JavaScript
const assert = require('node:assert/strict');
|
||
const { readFileSync } = require('node:fs');
|
||
const { join } = require('node:path');
|
||
const vm = require('node:vm');
|
||
const { test } = require('node:test');
|
||
|
||
const html = readFileSync(join(__dirname, '..', 'viewer', 'studio.html'), 'utf8');
|
||
const script = html.match(/<script>\n([\s\S]*?)<\/script>/)[1];
|
||
|
||
function fakeElement() {
|
||
return {
|
||
children: [], childNodes: [{ nodeValue: '' }], style: {}, markup: '', innerText: '', value: '', className: '',
|
||
get innerHTML() { return this.markup; },
|
||
set innerHTML(markup) { this.markup = markup; this.children = []; },
|
||
get classList() {
|
||
return {
|
||
add: name => { if (!this.className.split(' ').includes(name)) this.className += ` ${name}`; },
|
||
remove: name => { this.className = this.className.split(' ').filter(token => token !== name).join(' '); },
|
||
toggle: (name, force = !this.className.split(' ').includes(name)) =>
|
||
this.classList[force ? 'add' : 'remove'](name),
|
||
};
|
||
},
|
||
setAttribute() {},
|
||
focus() { this.focused = true; },
|
||
replaceChildren(...children) { this.children = children; },
|
||
appendChild(child) { this.children.push(child); },
|
||
get childElementCount() { return this.children.length; },
|
||
remove() {},
|
||
};
|
||
}
|
||
|
||
class Vector3 {
|
||
set(x, y, z) { Object.assign(this, { x, y, z }); return this; }
|
||
toArray() { return [this.x, this.y, this.z]; }
|
||
sub() { return this; }
|
||
}
|
||
class Disposable { disposed = false; dispose() { this.disposed = true; } }
|
||
|
||
function loadStudio() {
|
||
const elements = new Map(), loads = [], alerts = [];
|
||
const document = {
|
||
getElementById: id => elements.get(id) ?? elements.set(id, fakeElement()).get(id),
|
||
createElement: fakeElement,
|
||
querySelectorAll: selector => {
|
||
const found = [], visit = element => {
|
||
if (element.className.split(' ').includes(selector.slice(1))) found.push(element);
|
||
element.children.forEach(visit);
|
||
};
|
||
elements.forEach(visit);
|
||
return found;
|
||
},
|
||
};
|
||
const context = vm.createContext({
|
||
document, URLSearchParams, localStorage: { getItem: () => null, setItem() {} }, window: {}, setTimeout() {},
|
||
alert: message => alerts.push(message), console: { error() {} },
|
||
THREE: {
|
||
MeshStandardMaterial: Disposable, MeshNormalMaterial: Disposable, Vector3,
|
||
Box3: class {
|
||
setFromObject(model) { this.side = model.side; return this; }
|
||
getCenter(v) { return v.set(0, 0, 0); }
|
||
getSize(v) { return v.set(this.side, this.side, this.side); }
|
||
},
|
||
GLTFLoader: class { load(url, onLoad, onProgress, onError) { loads.push({ url, onLoad, onError }); } },
|
||
},
|
||
});
|
||
vm.runInContext(script, context);
|
||
vm.runInContext(`
|
||
scene = { objects: new Set(), add(o) { this.objects.add(o); }, remove(o) { this.objects.delete(o); } };
|
||
camera = { position: new THREE.Vector3().set(0, 2, 5) };
|
||
controls = {
|
||
target: new THREE.Vector3().set(0, 0, 0), update() {},
|
||
saveState() { this.eye = camera.position.toArray(); this.center = this.target.toArray(); },
|
||
reset() { camera.position.set(...this.eye); this.target.set(...this.center); },
|
||
};
|
||
controls.saveState();`, context);
|
||
return { context, elements, loads, alerts, run: code => vm.runInContext(code, context) };
|
||
}
|
||
|
||
function fakeModel(side = 1) {
|
||
const geometry = Object.assign(new Disposable(), { attributes: { position: { count: 3 } }, index: null });
|
||
const texture = Object.assign(new Disposable(), { isTexture: true });
|
||
const material = Object.assign(new Disposable(), { map: texture });
|
||
const mesh = { isMesh: true, geometry, material, userData: {} };
|
||
return { side, position: new Vector3(), userData: {}, parts: [geometry, material, texture],
|
||
traverse(visit) { visit(this); visit(mesh); } };
|
||
}
|
||
|
||
const HOSTILE = '"><img src=x onerror=alert(1)>';
|
||
const ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" };
|
||
const tagShape = markup => markup.match(/<[^>]*>/g).map(tag => tag.replace(/"[^"]*"/g, '""'));
|
||
const visibleText = markup => markup.replace(/<[^>]*>/g, '').replace(/&(?:#(\d+)|#x([\da-f]+)|([a-z]+));/gi,
|
||
(_, dec, hex, name) => dec ? String.fromCharCode(dec) : hex ? String.fromCharCode(parseInt(hex, 16)) : ENTITIES[name]);
|
||
|
||
function renderModelList(name) {
|
||
const studio = loadStudio();
|
||
studio.context.models = [{ name: name + '.glb', folder: name, category: name, size: 4, url: '/models/x.glb' }];
|
||
studio.run('allModels = models; renderAccordionList();');
|
||
const [group] = studio.elements.get('model-list').children;
|
||
return group.children[0].innerHTML + group.children[1].children.at(-1).children[0].innerHTML;
|
||
}
|
||
|
||
function renderTextureGrid(name) {
|
||
const studio = loadStudio();
|
||
studio.context.textures = Array.from({ length: 81 },
|
||
() => ({ name: name + '.png', format: 'PNG Image', size: 4, preview: '/textures/x.png' }));
|
||
studio.run('allTextures = textures; renderTexturesGrid();');
|
||
const grid = studio.elements.get('textures-grid');
|
||
grid.children.at(-1).onclick();
|
||
const cards = grid.children.filter(child => child.className === 'tex-card');
|
||
assert.equal(cards.length, 81);
|
||
return cards[0].innerHTML + cards[80].innerHTML;
|
||
}
|
||
|
||
test('hostile model and texture file names render as text, not as elements or attributes', () => {
|
||
for (const render of [renderModelList, renderTextureGrid]) {
|
||
const hostile = render(HOSTILE);
|
||
assert.deepEqual(tagShape(hostile), tagShape(render('plain name')));
|
||
assert.ok(visibleText(hostile).includes(HOSTILE), `${render.name} must still show the name`);
|
||
}
|
||
});
|
||
|
||
test('a texture preview that fails to load fires its error handler once, not in a loop', () => {
|
||
// Chromium fires "error" again for every src='' it is given, so a handler that sets src='' without clearing
|
||
// itself first runs forever (about 424,000 times in 3 s for one card in Chromium 156). This fake image fires the
|
||
// handler again on every src write, as that browser does, and both card paths (first 80, "show all") are used.
|
||
const cards = renderTextureGrid('plain name');
|
||
const handlers = [...cards.matchAll(/<img\b[^>]*\sonerror="([^"]*)"/g)].map(m => m[1]);
|
||
assert.equal(handlers.length, 2);
|
||
for (const code of handlers) {
|
||
let fired = 0;
|
||
const img = {
|
||
alt: '',
|
||
onerror: null,
|
||
set src(_value) { if (this.onerror && fired < 1000) { fired++; this.onerror(); } },
|
||
};
|
||
img.onerror = new Function(code);
|
||
fired++;
|
||
img.onerror();
|
||
assert.equal(fired, 1, `the handler ran ${fired} times: ${code}`);
|
||
assert.equal(img.alt, 'DDS / Pre-converted');
|
||
}
|
||
});
|
||
|
||
test('a download that finishes or fails after a later click does not replace the model last clicked', () => {
|
||
const studio = loadStudio();
|
||
studio.run(`loadModel('/models/a.glb', 'a.glb', '', 1);
|
||
loadModel('/models/b.glb', 'b.glb', '', 1);
|
||
loadModel('/models/c.glb', 'c.glb', '', 1);`);
|
||
const [a, b, c] = studio.loads;
|
||
const modelC = fakeModel();
|
||
c.onLoad({ scene: modelC });
|
||
b.onError(new Error('late failure'));
|
||
a.onLoad({ scene: fakeModel() });
|
||
assert.equal(studio.run('currentMesh'), modelC);
|
||
assert.deepEqual([...studio.run('scene.objects')], [modelC]);
|
||
assert.equal(studio.elements.get('stat-name').innerText, 'c.glb');
|
||
assert.deepEqual(studio.alerts, []);
|
||
});
|
||
|
||
test('a completed model timer leaves the next model loading bar visible', () => {
|
||
const studio = loadStudio();
|
||
const timers = [];
|
||
studio.context.setTimeout = callback => timers.push(callback);
|
||
|
||
studio.run("loadModel('/models/a.glb', 'a.glb', '', 1);");
|
||
studio.loads[0].onLoad({ scene: fakeModel() });
|
||
studio.run("loadModel('/models/b.glb', 'b.glb', '', 1);");
|
||
|
||
const bar = studio.elements.get('loading-bar');
|
||
assert.equal(bar.style.transform, 'scaleX(0.4)');
|
||
timers[0]();
|
||
assert.equal(bar.style.transform, 'scaleX(0.4)');
|
||
|
||
studio.loads[1].onLoad({ scene: fakeModel() });
|
||
assert.equal(bar.style.transform, 'scaleX(1)');
|
||
timers[1]();
|
||
assert.equal(bar.style.transform, 'scaleX(0)');
|
||
});
|
||
|
||
test('switching models in clay mode disposes the old model\'s geometry, material and texture, not the clay', () => {
|
||
const studio = loadStudio();
|
||
const [modelA, modelB] = [fakeModel(), fakeModel()];
|
||
studio.run(`setShadingMode('clay'); loadModel('/models/a.glb', 'a.glb', '', 1);`);
|
||
studio.loads[0].onLoad({ scene: modelA });
|
||
studio.run(`loadModel('/models/b.glb', 'b.glb', '', 1);`);
|
||
studio.loads[1].onLoad({ scene: modelB });
|
||
assert.deepEqual(modelA.parts.map(part => part.disposed), [true, true, true]);
|
||
assert.deepEqual(modelB.parts.map(part => part.disposed), [false, false, false]);
|
||
assert.equal(studio.run('clayMaterial.disposed'), false);
|
||
});
|
||
|
||
test('Reset Camera returns to the fitted view of the currently loaded model', () => {
|
||
const studio = loadStudio();
|
||
let previousEye = studio.run('camera.position.toArray()');
|
||
for (const side of [20, 4]) {
|
||
studio.run(`loadModel('/models/box.glb', 'box.glb', '', 1)`);
|
||
studio.loads.at(-1).onLoad({ scene: fakeModel(side) });
|
||
const eye = studio.run('camera.position.toArray()');
|
||
const target = studio.run('controls.target.toArray()');
|
||
assert.notDeepEqual(eye, previousEye);
|
||
studio.run('camera.position.set(30, 5, 25); controls.target.set(1, 1, 1); resetCamera()');
|
||
assert.deepEqual(studio.run('camera.position.toArray()'), eye);
|
||
assert.deepEqual(studio.run('controls.target.toArray()'), target);
|
||
previousEye = eye;
|
||
}
|
||
});
|
||
|
||
test('paging a large model category keeps one page and restores the selected row when returning', () => {
|
||
const studio = loadStudio();
|
||
studio.context.models = Array.from({ length: 10001 }, (_, i) => ({
|
||
name: `model-${i}.glb`, category: 'Props', folder: 'Synthetic', size: 4, url: `/models/${i}.glb`,
|
||
}));
|
||
studio.run('allModels = models; renderAccordionList();');
|
||
const content = studio.elements.get('model-list').children[0].children[1];
|
||
const [control, page] = content.children;
|
||
const [previous, range, next] = control.children;
|
||
const rows = () => page.children;
|
||
|
||
assert.equal(rows().length, 100);
|
||
rows()[30].onclick();
|
||
next.onclick();
|
||
assert.equal(rows().length, 100);
|
||
assert.match(rows()[0].innerHTML, /model-100\.glb/);
|
||
assert.equal(range.innerText, '101–200 / 10001');
|
||
assert.equal(previous.disabled, false);
|
||
|
||
previous.onclick();
|
||
assert.equal(rows().length, 100);
|
||
assert.match(rows()[30].className, /\bactive\b/);
|
||
assert.equal(previous.disabled, true);
|
||
assert.deepEqual(studio.loads.map(load => load.url), ['/models/30.glb']);
|
||
|
||
studio.context.query = 'model-10000.glb';
|
||
studio.run(`document.getElementById('model-search').value = query; onModelSearch();`);
|
||
const filtered = studio.elements.get('model-list').children[0].children[1];
|
||
assert.match(filtered.children[0].children[0].innerHTML, /model-10000\.glb/);
|
||
assert.equal(filtered.children.length, 1);
|
||
});
|
||
|
||
test('switching to EU relabels the stats panel, the buttons, the texture gallery and the empty lists', async () => {
|
||
const studio = loadStudio();
|
||
const ids = ['stat-name', 'stat-category', 'stat-triangles', 'stat-vertices', 'stat-size'];
|
||
// In the page each label is the text node in front of its value: <div>Модель: <b id="stat-name">.
|
||
const labels = ids.map(id => (studio.context.document.getElementById(id).previousSibling = { nodeValue: '' }));
|
||
studio.context.fetch = async () => ({ json: async () => ({ textures: [] }) });
|
||
await studio.run('loadTextures()');
|
||
studio.run('renderCategoryChips({}); renderAccordionList();');
|
||
const allChip = studio.elements.get('category-chips').children[0];
|
||
studio.elements.set('chip-all', allChip);
|
||
assert.equal(allChip.innerText, 'Все (0)');
|
||
assert.equal(studio.elements.get('tex-count').innerText, '0 текстур найдено');
|
||
assert.equal(visibleText(studio.elements.get('textures-grid').innerHTML), 'Текстуры не найдены');
|
||
assert.equal(visibleText(studio.elements.get('model-list').innerHTML), 'Ничего не найдено');
|
||
|
||
studio.run(`setLanguage('eu')`);
|
||
assert.deepEqual(labels.map(label => label.nodeValue),
|
||
['Model: ', 'Category: ', 'Polygons (triangles): ', 'Vertices: ', 'File size: ']);
|
||
assert.equal(studio.elements.get('btn-reset-camera').innerText, 'Reset Camera');
|
||
assert.equal(studio.elements.get('tex-heading').innerText, 'Texture Gallery (DDS & PNG)');
|
||
assert.equal(allChip.innerText, 'All (0)');
|
||
assert.equal(studio.elements.get('tex-count').innerText, '0 textures found');
|
||
assert.equal(visibleText(studio.elements.get('textures-grid').innerHTML), 'No textures found');
|
||
assert.equal(visibleText(studio.elements.get('model-list').innerHTML), 'No models found');
|
||
|
||
studio.context.textures = Array.from({ length: 81 }, () => ({ name: 'a.png', format: 'PNG Image', size: 4, preview: '/a.png' }));
|
||
studio.run('allTextures = textures; renderTexturesGrid();');
|
||
assert.equal(visibleText(studio.elements.get('textures-grid').children.at(-1).innerHTML), 'Show all (1 more)');
|
||
studio.context.fetch = async () => { throw new Error('offline'); };
|
||
await studio.run('loadTextures()');
|
||
assert.equal(studio.elements.get('tex-count').innerText, 'Could not load the textures');
|
||
});
|
||
|
||
test('each model category pages independently, including a partial last page', () => {
|
||
const studio = loadStudio();
|
||
studio.context.models = ['Props', 'Vehicles'].flatMap(category => Array.from({ length: 101 }, (_, i) => ({
|
||
name: `${category}-${i}.glb`, category, folder: 'Synthetic', size: 4,
|
||
})));
|
||
studio.run('currentLang = "eu"; allModels = models; renderAccordionList();');
|
||
const [props, vehicles] = studio.elements.get('model-list').children;
|
||
const [controls, page] = props.children[1].children;
|
||
const [previous, range, next] = controls.children;
|
||
assert.equal(previous.innerText, 'Previous');
|
||
assert.equal(next.innerText, 'Next');
|
||
next.onclick();
|
||
assert.equal(page.children.length, 1);
|
||
assert.match(page.children[0].innerHTML, /Props-100\.glb/);
|
||
assert.equal(range.innerText, '101–101 / 101');
|
||
assert.equal(next.disabled, true);
|
||
assert.equal(previous.focused, true);
|
||
assert.match(vehicles.children[1].children[1].children[0].innerHTML, /Vehicles-0\.glb/);
|
||
});
|
||
|
||
test('language and equivalent search text preserve model pages and the selected row', () => {
|
||
const studio = loadStudio();
|
||
studio.context.models = ['Props', 'Vehicles'].flatMap(category => Array.from({ length: 201 }, (_, i) => ({
|
||
name: `${category}-${i}.glb`, category, folder: 'Synthetic', size: 4, url: `/${category}/${i}.glb`,
|
||
})));
|
||
studio.run(`allModels = models;
|
||
document.getElementById('model-search').value = 'synthetic'; onModelSearch();`);
|
||
const groups = () => studio.elements.get('model-list').children.map(group => group.children[1]);
|
||
const [props, vehicles] = groups();
|
||
props.children[0].children[2].onclick();
|
||
vehicles.children[0].children[2].onclick();
|
||
vehicles.children[0].children[2].onclick();
|
||
props.children.at(-1).children[0].onclick();
|
||
|
||
studio.run(`setLanguage('eu');`);
|
||
const [translatedProps, translatedVehicles] = groups();
|
||
assert.equal(translatedProps.children[0].children[1].innerText, '101–200 / 201');
|
||
assert.equal(translatedVehicles.children[0].children[1].innerText, '201–201 / 201');
|
||
assert.equal(translatedProps.children[0].children[2].innerText, 'Next');
|
||
assert.match(translatedProps.children.at(-1).children[0].className, /\bactive\b/);
|
||
|
||
studio.run(`document.getElementById('model-search').value = ' SYNTHETIC '; onModelSearch();`);
|
||
assert.equal(groups()[0], translatedProps);
|
||
assert.deepEqual(studio.loads.map(load => load.url), ['/Props/100.glb']);
|
||
|
||
studio.run(`document.getElementById('model-search').value = 'Props'; onModelSearch();`);
|
||
assert.equal(groups().length, 1);
|
||
assert.equal(groups()[0].children[0].children[1].innerText, '1–100 / 201');
|
||
});
|
||
|
||
test('a failed download selects the row of the model still shown, also after the list is redrawn', () => {
|
||
const studio = loadStudio();
|
||
studio.context.models = ['a', 'b', 'c'].map(name => ({
|
||
name: `${name}.glb`, category: 'Props', folder: 'Synthetic', size: 4, url: `/models/${name}.glb`,
|
||
}));
|
||
const rows = () => studio.elements.get('model-list').children[0].children[1].children.at(-1).children;
|
||
const active = () => rows().flatMap((row, i) => row.className.split(' ').includes('active') ? ['abc'[i]] : []);
|
||
studio.run('allModels = models; renderAccordionList();');
|
||
|
||
rows()[0].onclick();
|
||
studio.loads[0].onError(new Error('first download failed'));
|
||
assert.deepEqual(active(), [], 'nothing is shown yet');
|
||
|
||
rows()[0].onclick();
|
||
studio.loads[1].onLoad({ scene: fakeModel() });
|
||
const list = rows();
|
||
rows()[1].onclick();
|
||
assert.deepEqual(active(), ['b']);
|
||
studio.loads[2].onError(new Error('failed'));
|
||
assert.equal(studio.elements.get('stat-name').innerText, 'a.glb');
|
||
assert.deepEqual(active(), ['a']);
|
||
assert.equal(rows(), list, 'the list is not rebuilt, so its scroll and collapsed groups stay');
|
||
studio.run('renderAccordionList();');
|
||
assert.deepEqual(active(), ['a']);
|
||
|
||
// A download that a later click replaced leaves the later selection alone, and the last failure goes back to a.
|
||
rows()[1].onclick();
|
||
rows()[2].onclick();
|
||
studio.loads[3].onError(new Error('late failure'));
|
||
assert.deepEqual(active(), ['c']);
|
||
studio.loads[4].onError(new Error('failed'));
|
||
assert.deepEqual(active(), ['a']);
|
||
|
||
rows()[1].onclick();
|
||
studio.loads[5].onLoad({ scene: fakeModel() });
|
||
assert.deepEqual(active(), ['b']);
|
||
assert.equal(studio.alerts.length, 3);
|
||
});
|
||
|
||
test('DDS inspector selects a mip and layer and clears controls for ordinary images', async () => {
|
||
const { context, elements, run } = loadStudio();
|
||
context.fetch = async () => ({ ok: true, json: async () => ({ width: 5, height: 3, mip_count: 3, array_size: 2 }) });
|
||
await run("openModal({name:'a', preview:'/a.png', ddsPreview:'/api/dds/a.dds'})");
|
||
const image = elements.get('modal-img');
|
||
const controls = elements.get('dds-controls');
|
||
const [mip, layer, dimensions] = controls.children;
|
||
assert.equal(image.src, '/api/dds/a.dds?mip=0&layer=0');
|
||
for (const label of [mip, layer]) {
|
||
const input = label.children[0];
|
||
Object.assign(input, {value: '1', valueAsNumber: 1, validity: {valid: true}});
|
||
input.onchange();
|
||
}
|
||
assert.equal(image.src, '/api/dds/a.dds?mip=1&layer=1');
|
||
assert.equal(dimensions.textContent, '2 \u00d7 1');
|
||
await run("openModal({name:'b', preview:'/b.png'})");
|
||
assert.equal(image.src, '/b.png');
|
||
assert.equal(controls.children.length, 0);
|
||
});
|
||
|
||
test('cube inspector requests the selected face for both preview and texel', async () => {
|
||
const { context, elements, run } = loadStudio();
|
||
const info = {format: 'R16_FLOAT', width: 1, height: 1, mip_count: 1, array_size: 1, faces: ['ny', 'nz']};
|
||
const requests = [];
|
||
context.fetch = async url => {
|
||
requests.push(url);
|
||
return {ok: true, json: async () => url.endsWith('?info') ? info : {R: '0.5'}};
|
||
};
|
||
await run("openModal({name:'cube', preview:'/c.png', ddsPreview:'/api/dds/c.dds'})");
|
||
const select = elements.get('dds-controls').children[0].children[0];
|
||
assert.deepEqual(select.children.map(option => option.value), ['ny', 'nz']);
|
||
assert.match(elements.get('modal-img').src, /face=ny$/);
|
||
select.value = 'nz';
|
||
select.onchange();
|
||
assert.match(elements.get('modal-img').src, /face=nz$/);
|
||
assert.match(requests.at(-1), /face=nz&x=0&y=0$/);
|
||
});
|
||
|
||
test('volume slices shrink with the selected mip', async () => {
|
||
const { context, elements, run } = loadStudio();
|
||
const info = {dimension: '3d', width: 9, height: 3, depth: 5, mip_count: 3, array_size: 1};
|
||
context.fetch = async () => ({ok: true, json: async () => info});
|
||
await run("openModal({name:'v', preview:'/v.png', ddsPreview:'/api/dds/v.dds'})");
|
||
const [mip, slice, dimensions] = elements.get('dds-controls').children;
|
||
const z = slice.children[0];
|
||
const image = elements.get('modal-img');
|
||
function select(input, value) {
|
||
Object.assign(input, {value: String(value), valueAsNumber: value, validity: {valid: true}});
|
||
input.onchange();
|
||
}
|
||
assert.equal(z.max, 4);
|
||
select(z, 4);
|
||
assert.equal(image.src, '/api/dds/v.dds?mip=0&z=4');
|
||
select(mip.children[0], 1);
|
||
assert.equal(z.max, 1);
|
||
assert.equal(z.value, 1);
|
||
assert.equal(dimensions.textContent, '4 \u00d7 1');
|
||
assert.equal(image.src, '/api/dds/v.dds?mip=1&z=1');
|
||
select(mip.children[0], 2);
|
||
assert.equal(z.disabled, true);
|
||
assert.equal(image.src, '/api/dds/v.dds?mip=2&z=0');
|
||
select(mip.children[0], 0);
|
||
assert.equal(z.disabled, false);
|
||
});
|
||
|
||
test('half-float coordinates follow the selected mip and late reads cannot replace its value', async () => {
|
||
const { context, elements, run } = loadStudio();
|
||
const info = {format: 'R16_FLOAT', width: 5, height: 3, mip_count: 2, array_size: 1};
|
||
const pending = [];
|
||
context.fetch = url => url.endsWith('?info') ? {ok: true, json: async () => info} :
|
||
new Promise(resolve => pending.push({url, resolve}));
|
||
await run("openModal({name:'a', preview:'/a.png', ddsPreview:'/api/dds/a.dds'})");
|
||
const [mip, x, y, output] = elements.get('dds-controls').children;
|
||
x.children[0].value = 4;
|
||
y.children[0].value = 2;
|
||
const older = x.children[0].onchange();
|
||
assert.match(pending[1].url, /mip=0&layer=0&x=4&y=2$/);
|
||
Object.assign(mip.children[0], {value: '1', valueAsNumber: 1, validity: {valid: true}});
|
||
mip.children[0].onchange();
|
||
assert.match(pending[2].url, /mip=1&layer=0&x=1&y=0$/);
|
||
pending[2].resolve({ok: true, json: async () => ({R: '-2.5'})});
|
||
await new Promise(resolve => setImmediate(resolve));
|
||
pending[1].resolve({ok: false});
|
||
await older;
|
||
assert.equal(output.textContent, 'R = -2.5');
|
||
});
|
||
|
||
test('single-mip single-layer DDS still shows dimensions without adding inputs', async () => {
|
||
const { context, elements, run } = loadStudio();
|
||
const info = {width: 32, height: 16, mip_count: 1, array_size: 1, format: 'BC1_UNORM'};
|
||
const requests = [];
|
||
context.fetch = async url => {
|
||
requests.push(url);
|
||
return {ok: true, json: async () => info};
|
||
};
|
||
await run("openModal({name:'a', preview:'/a.png', ddsPreview:'/api/dds/a.dds'})");
|
||
const image = elements.get('modal-img');
|
||
const controls = elements.get('dds-controls');
|
||
assert.deepEqual(requests, ['/api/dds/a.dds?info']);
|
||
assert.equal(controls.children.length, 1);
|
||
assert.equal(controls.children[0].textContent, '32 \u00d7 16');
|
||
assert.equal(controls.children[0].children.length, 0);
|
||
assert.equal(image.src, '/a.png');
|
||
});
|
||
|
||
test('hostile texture preview URLs and format labels render escaped across both batches', () => {
|
||
function renderCards(preview, format) {
|
||
const studio = loadStudio();
|
||
studio.context.textures = Array.from({ length: 81 },
|
||
() => ({ name: 'plain.png', format, size: 4, preview }));
|
||
studio.run('allTextures = textures; renderTexturesGrid();');
|
||
const grid = studio.elements.get('textures-grid');
|
||
grid.children.at(-1).onclick();
|
||
const cards = grid.children.filter(child => child.className === 'tex-card');
|
||
assert.equal(cards.length, 81);
|
||
return [cards[0].innerHTML, cards[80].innerHTML];
|
||
}
|
||
|
||
const [plain0, plain80] = renderCards('/textures/x.png', 'PNG Image');
|
||
const plainShape0 = tagShape(plain0);
|
||
const plainShape80 = tagShape(plain80);
|
||
for (const [label, preview, format] of [
|
||
['preview', '/textures/' + HOSTILE, 'PNG Image'],
|
||
['format', '/textures/x.png', HOSTILE],
|
||
]) {
|
||
const cards = renderCards(preview, format);
|
||
assert.deepEqual(tagShape(cards[0]), plainShape0, `${label} in initial batch injected markup`);
|
||
assert.deepEqual(tagShape(cards[1]), plainShape80, `${label} in show-all batch injected markup`);
|
||
for (const markup of cards) {
|
||
assert.ok(!markup.includes(HOSTILE), `${label} rendered unescaped hostile string`);
|
||
if (label === 'format') {
|
||
assert.ok(visibleText(markup).includes(HOSTILE), 'escaped format must preserve visible text');
|
||
}
|
||
}
|
||
}
|
||
});
|
||
|
||
// A model as GLTFLoader returns it: two meshes share Body, a third has a material array, one node is not a mesh.
|
||
function inspectedModel(bodyName = 'Body') {
|
||
const texture = image => ({ isTexture: true, image, dispose() {} });
|
||
const albedo = texture({ width: 1024, height: 512 });
|
||
const body = { name: bodyName, map: albedo, normalMap: texture({ naturalWidth: 512, naturalHeight: 512, width: 0 }),
|
||
roughnessMap: null, emissiveMap: albedo, aoMap: texture(null), dispose() {} };
|
||
const flat = { name: '', vertexColors: false, map: null, dispose() {} };
|
||
const painted = { name: 'Decals', vertexColors: true, map: null, dispose() {} };
|
||
const mesh = material => ({ isMesh: true, material, userData: {},
|
||
geometry: { attributes: { position: { count: 3 } }, index: null, dispose() {} } });
|
||
// disposeModel calls dispose() on a material array itself (claimed separately as VIEWER-DISPOSE-RESOURCES), so the
|
||
// array carries one: these tests are about the details panel, not disposal.
|
||
const nodes = [mesh(body), mesh(body), mesh(Object.assign([flat, painted], { dispose() {} })),
|
||
{ isMesh: false, userData: {} }];
|
||
return { position: new Vector3(), userData: {}, traverse(visit) { visit(this); nodes.forEach(visit); } };
|
||
}
|
||
|
||
function materialRows(studio) {
|
||
return studio.elements.get('stat-material-list').innerHTML.split('<div class="mat-row">').slice(1);
|
||
}
|
||
|
||
test('the stats panel lists the model\'s size, meshes, materials and the textures each material uses', () => {
|
||
const studio = loadStudio();
|
||
studio.context.THREE.Box3 = class {
|
||
setFromObject() { return this; }
|
||
getCenter(v) { return v; }
|
||
getSize(v) { return Object.assign(v, { x: 4.5234, y: 1.4, z: 0.021549 }); }
|
||
};
|
||
studio.run(`setShadingMode('clay'); loadModel('/models/car.glb', 'car.glb', '', 1);`);
|
||
studio.loads[0].onLoad({ scene: inspectedModel() });
|
||
assert.equal(studio.elements.get('stat-dimensions').innerText, '4.52 × 1.4 × 0.0215 m');
|
||
// 3 meshes, 3 materials (clay mode shows the model's own, not the studio clay), 3 textures (albedo is used twice).
|
||
assert.equal(studio.elements.get('stat-parts').innerText, '3 / 3 / 3');
|
||
assert.equal(studio.elements.get('stat-materials').hidden, false);
|
||
const rows = materialRows(studio);
|
||
assert.deepEqual(rows.map(visibleText), [
|
||
'Body · мешей: 2map 1024×512normalMap 512×512emissiveMap 1024×512aoMap ?',
|
||
'(без имени) · мешей: 1без текстур',
|
||
'Decals · мешей: 1цвета вершин',
|
||
]);
|
||
// Only the material with neither a texture nor vertex colours is marked.
|
||
assert.deepEqual(rows.map(row => row.includes('class="mat-none"')), [false, true, false]);
|
||
});
|
||
|
||
test('material names render as text and the details follow the language switch', () => {
|
||
const studio = loadStudio();
|
||
studio.run(`loadModel('/models/x.glb', 'x.glb', '', 1);`);
|
||
studio.loads[0].onLoad({ scene: inspectedModel(HOSTILE) });
|
||
const [hostile] = materialRows(studio);
|
||
assert.deepEqual(tagShape(hostile), ['<b>', '</b>', '<div class="">', '</div>', '<div class="">', '</div>',
|
||
'<div class="">', '</div>', '<div class="">', '</div>', '</div>']);
|
||
assert.ok(visibleText(hostile).startsWith(HOSTILE + ' · '));
|
||
studio.run(`setLanguage('eu')`);
|
||
assert.equal(studio.elements.get('t-stat-dimensions').innerText, 'Size (W × H × D):');
|
||
assert.equal(studio.elements.get('t-stat-parts').innerText, 'Meshes / materials / textures:');
|
||
assert.equal(studio.elements.get('t-stat-materials').innerText, 'Materials and textures');
|
||
assert.deepEqual(materialRows(studio).slice(1).map(visibleText),
|
||
['(unnamed) · meshes: 1no texture', 'Decals · meshes: 1vertex colours']);
|
||
});
|
||
|
||
test('the details belong to the model shown: a failed load keeps them, an empty model hides the material list', () => {
|
||
const studio = loadStudio();
|
||
studio.run('renderModelDetails()');
|
||
assert.equal(studio.elements.get('stat-parts').innerText, '—');
|
||
assert.equal(studio.elements.get('stat-materials').hidden, true);
|
||
studio.run(`loadModel('/models/a.glb', 'a.glb', '', 1); loadModel('/models/b.glb', 'b.glb', '', 1);`);
|
||
studio.loads[1].onLoad({ scene: inspectedModel() });
|
||
studio.loads[0].onLoad({ scene: inspectedModel('stale') });
|
||
studio.run(`loadModel('/models/c.glb', 'c.glb', '', 1);`);
|
||
studio.loads[2].onError(new Error('download failed'));
|
||
assert.equal(studio.elements.get('stat-parts').innerText, '3 / 3 / 3');
|
||
assert.ok(visibleText(materialRows(studio)[0]).startsWith('Body · '));
|
||
// The default Box3 stub gives no numeric size: the panel says so instead of printing NaN.
|
||
assert.equal(studio.elements.get('stat-dimensions').innerText, '—');
|
||
studio.run(`loadModel('/models/empty.glb', 'empty.glb', '', 1);`);
|
||
studio.loads[3].onLoad({ scene: { position: new Vector3(), userData: {}, traverse(visit) { visit(this); } } });
|
||
assert.equal(studio.elements.get('stat-parts').innerText, '0 / 0 / 0');
|
||
assert.equal(studio.elements.get('stat-materials').hidden, true);
|
||
assert.equal(studio.elements.get('stat-material-list').innerHTML, '');
|
||
});
|