Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/test_viewer_studio.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,42 @@ function fakeModel() {
traverse(visit) { visit(this); visit(mesh); } };
}

test('reopening the texture tab reuses an in-flight request and a successful empty list', async () => {
const requests = [];
const studio = loadStudio();
studio.context.fetch = url => new Promise(resolve => requests.push({ url, resolve }));
const reopen = () => studio.run("switchTab('models'); switchTab('textures');");

reopen();
reopen();
assert.deepEqual(requests.map(request => request.url), ['/api/textures']);
requests[0].resolve({ ok: true, json: async () => ({ textures: [] }) });
await new Promise(setImmediate);

assert.equal(studio.elements.get('badge-textures').innerText, 0);
reopen();
assert.equal(requests.length, 1);
});

test('reopening the texture tab retries a failed request', async () => {
let attempts = 0;
const studio = loadStudio();
studio.context.fetch = async () => ({
ok: ++attempts > 1,
status: attempts === 1 ? 503 : 200,
json: async () => ({ textures: [{ name: 'sample.png', format: 'PNG Image', size: 4 }] }),
});

studio.run("switchTab('textures');");
await new Promise(setImmediate);
assert.notEqual(studio.elements.get('badge-textures')?.innerText, 1);

studio.run("switchTab('models'); switchTab('textures');");
await new Promise(setImmediate);
assert.equal(attempts, 2);
assert.equal(studio.elements.get('badge-textures').innerText, 1);
});

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, '""'));
Expand Down
9 changes: 8 additions & 1 deletion viewer/studio.html
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ <h2 style="font-size: 16px; font-weight: 700; color: var(--accent-pink);">Гал
} else {
document.getElementById('tab-btn-textures').classList.add('active');
document.getElementById('textures-view').classList.add('active');
if (allTextures.length === 0) loadTextures();
loadTextures();
}
}

Expand Down Expand Up @@ -954,15 +954,22 @@ <h2 style="font-size: 16px; font-weight: 700; color: var(--accent-pink);">Гал
}

// Textures View Logic
let textureListStatus = 'idle';

async function loadTextures() {
if (textureListStatus !== 'idle') return;
textureListStatus = 'loading';
try {
const res = await fetch('/api/textures');
if (!res.ok) throw new Error(`Texture list request failed: HTTP ${res.status}`);
const data = await res.json();
allTextures = data.textures || [];
textureListStatus = 'loaded';
document.getElementById('badge-textures').innerText = allTextures.length;
document.getElementById('tex-count').innerText = `${allTextures.length} текстур найдено`;
renderTexturesGrid();
} catch (e) {
textureListStatus = 'idle';
console.error('Failed to load textures:', e);
document.getElementById('tex-count').innerText = 'Ошибка загрузки текстур';
}
Expand Down