165 lines
7.0 KiB
JavaScript
165 lines
7.0 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { JSDOM } from 'jsdom';
|
|
import EventEmitter from 'node:events';
|
|
|
|
class MockSocket extends EventEmitter {
|
|
constructor() {
|
|
super();
|
|
this.OPEN = 1;
|
|
this.readyState = 1;
|
|
this.sent = [];
|
|
}
|
|
send(data) {
|
|
this.sent.push(data);
|
|
}
|
|
}
|
|
|
|
test('Frontend - Downloads Sob Demanda, Catálogo em Memória e Alternância Voz/Envio', async () => {
|
|
const html = `
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<body>
|
|
<div class="chat-header">
|
|
<button id="btn-session-downloads">
|
|
<span id="session-downloads-badge" style="display: none;">0</span>
|
|
</button>
|
|
<button type="button" id="btn-tui-toggle">⌨️</button>
|
|
<button id="chat-notif-toggle-btn">🔕</button>
|
|
<button id="btn-restart-terminal"></button>
|
|
<button id="btn-refresh-chat"></button>
|
|
<button id="btn-close-chat">×</button>
|
|
</div>
|
|
|
|
<div id="session-downloads-modal" style="display: none;">
|
|
<span id="session-downloads-count">0 arquivos detectados</span>
|
|
<button id="btn-close-session-downloads">×</button>
|
|
<div id="session-downloads-empty" style="display: block;"></div>
|
|
<div id="session-downloads-list" style="display: none;"></div>
|
|
</div>
|
|
|
|
<div id="file-preview-modal" style="display: none;">
|
|
<span id="file-preview-icon"></span>
|
|
<h3 id="file-preview-title"></h3>
|
|
<span id="file-preview-meta"></span>
|
|
<div id="file-preview-body"></div>
|
|
<button id="btn-copy-file-content" style="display: none;"></button>
|
|
<button id="btn-save-file"></button>
|
|
<button id="btn-close-file-preview">×</button>
|
|
</div>
|
|
|
|
<div id="attach-menu" style="display: none;">
|
|
<button type="button" id="btn-attach-camera"></button>
|
|
<button type="button" id="btn-attach-gallery"></button>
|
|
<button type="button" id="btn-attach-file"></button>
|
|
<button type="button" id="btn-menu-downloads"></button>
|
|
</div>
|
|
<div id="attach-backdrop" style="display: none;"></div>
|
|
|
|
<div class="chat-messages" id="chat-messages"></div>
|
|
|
|
<form class="chat-input-bar" id="chat-form">
|
|
<input type="file" id="chat-camera-input" style="display: none;">
|
|
<input type="file" id="chat-gallery-input" style="display: none;">
|
|
<input type="file" id="chat-file-input" style="display: none;">
|
|
|
|
<button type="button" id="btn-attach"></button>
|
|
<input type="text" id="chat-input" placeholder="Digite...">
|
|
<button type="button" id="btn-voice" style="display: inline-flex;"></button>
|
|
<button type="submit" id="btn-chat-send" style="display: none;"></button>
|
|
</form>
|
|
</body>
|
|
</html>
|
|
`;
|
|
|
|
const dom = new JSDOM(html);
|
|
global.window = dom.window;
|
|
global.document = dom.window.document;
|
|
global.WebSocket = MockSocket;
|
|
|
|
// Importa a lógica do frontend
|
|
await import('../public/app.js');
|
|
|
|
const {
|
|
sessionFilesCatalog,
|
|
registerSessionFiles,
|
|
updateSessionDownloadsBadge,
|
|
openSessionDownloadsModal,
|
|
closeSessionDownloadsModal,
|
|
updateInputActionButtonState
|
|
} = dom.window;
|
|
|
|
assert.ok(sessionFilesCatalog instanceof Map, 'sessionFilesCatalog deve ser uma instância de Map');
|
|
assert.equal(typeof registerSessionFiles, 'function', 'registerSessionFiles deve ser função');
|
|
assert.equal(typeof openSessionDownloadsModal, 'function', 'openSessionDownloadsModal deve ser função');
|
|
assert.equal(typeof closeSessionDownloadsModal, 'function', 'closeSessionDownloadsModal deve ser função');
|
|
assert.equal(typeof updateInputActionButtonState, 'function', 'updateInputActionButtonState deve ser função');
|
|
|
|
const badge = dom.window.document.getElementById('session-downloads-badge');
|
|
const modal = dom.window.document.getElementById('session-downloads-modal');
|
|
const emptyEl = dom.window.document.getElementById('session-downloads-empty');
|
|
const listEl = dom.window.document.getElementById('session-downloads-list');
|
|
const chatInput = dom.window.document.getElementById('chat-input');
|
|
const btnVoice = dom.window.document.getElementById('btn-voice');
|
|
const btnSend = dom.window.document.getElementById('btn-chat-send');
|
|
const chatForm = dom.window.document.getElementById('chat-form');
|
|
|
|
// 1. Estado inicial de alternância Voz / Envio
|
|
updateInputActionButtonState();
|
|
assert.equal(btnVoice.style.display, 'inline-flex', 'Microfone deve estar visível com input vazio');
|
|
assert.equal(btnSend.style.display, 'none', 'Envio deve estar oculto com input vazio');
|
|
|
|
// Ao digitar texto no input:
|
|
chatInput.value = 'Olá agente';
|
|
updateInputActionButtonState();
|
|
assert.equal(btnVoice.style.display, 'none', 'Microfone deve sumir quando há texto');
|
|
assert.equal(btnSend.style.display, 'inline-flex', 'Botão de envio deve aparecer quando há texto');
|
|
|
|
// Ao limpar o input:
|
|
chatInput.value = '';
|
|
updateInputActionButtonState();
|
|
assert.equal(btnVoice.style.display, 'inline-flex', 'Microfone reaparece após limpar texto');
|
|
assert.equal(btnSend.style.display, 'none', 'Botão de envio oculta novamente');
|
|
|
|
// 2. Registro transparente de arquivos no catálogo da sessão
|
|
sessionFilesCatalog.clear();
|
|
assert.equal(sessionFilesCatalog.size, 0);
|
|
assert.equal(badge.style.display, 'none');
|
|
|
|
registerSessionFiles([
|
|
'docs/changes/0-ux-downloads-sob-demanda-layout-input/spec.md',
|
|
'evidence/screen.png',
|
|
'public/bundles/manifest.json'
|
|
], 'test');
|
|
|
|
assert.equal(sessionFilesCatalog.size, 3, 'Deve registrar 3 arquivos no catálogo');
|
|
assert.equal(badge.style.display, 'inline-flex', 'Badge deve ficar visível');
|
|
assert.equal(badge.textContent, '3', 'Badge deve exibir contagem 3');
|
|
|
|
// 3. Abertura do Modal de Downloads Sob Demanda
|
|
openSessionDownloadsModal();
|
|
assert.equal(modal.style.display, 'flex', 'Modal de downloads deve abrir com display flex');
|
|
assert.equal(emptyEl.style.display, 'none', 'Estado vazio deve ser ocultado');
|
|
assert.equal(listEl.style.display, 'flex', 'Lista de arquivos deve ser exibida');
|
|
|
|
const items = listEl.querySelectorAll('.session-download-item');
|
|
assert.equal(items.length, 3, 'Deve renderizar 3 itens na lista de downloads');
|
|
const actionBtn = items[0].querySelector('.btn-session-download-action');
|
|
assert.ok(actionBtn, 'Cada item deve ter botão de ação Ver / Baixar');
|
|
assert.ok(actionBtn.textContent.includes('Ver / Baixar'));
|
|
|
|
// Fechamento do modal
|
|
closeSessionDownloadsModal();
|
|
assert.equal(modal.style.display, 'none', 'Modal de downloads deve fechar');
|
|
|
|
// 4. Interceptação de comando /downloads no chatForm
|
|
chatInput.value = '/downloads';
|
|
const submitEvent = new dom.window.Event('submit', { cancelable: true });
|
|
chatForm.dispatchEvent(submitEvent);
|
|
|
|
assert.equal(chatInput.value, '', 'Input deve ser limpo ao executar comando /downloads');
|
|
assert.equal(modal.style.display, 'flex', 'Comando /downloads deve abrir o modal de downloads sob demanda');
|
|
|
|
closeSessionDownloadsModal();
|
|
});
|