maestri-remote/test/native-download.test.js
Winícius 68ae249c33 fix(download): salvar no app Android so o arquivo escolhido na pre-visualizacao via plugin nativo
WebView do Capacitor ignora link blob: com download; botao Baixar da pre-visualizacao passa a usar o plugin NativeDownload (MediaStore/Downloads). Sem salvamento automatico.

Validacao: node --test test/native-download.test.js 2/2 ok; npm test 157/160, as 3 falhas (navigator JSDOM no Node 22 e sanitizacao de video no gateway) existem igualmente no HEAD anterior.
2026-11-22 23:38:34 -03:00

104 lines
4.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { JSDOM } from 'jsdom';
const html = `
<!DOCTYPE html>
<html>
<body>
<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">&times;</button>
</div>
<div class="chat-messages" id="chat-messages"></div>
<form id="chat-form"><input type="text" id="chat-input"></form>
</body>
</html>
`;
function setGlobal(name, value) {
Object.defineProperty(globalThis, name, { value, configurable: true, writable: true });
}
test('Pré-visualização: no app Android, "Baixar" salva pelo plugin nativo NativeDownload só o arquivo escolhido', async () => {
const dom = new JSDOM(html, { url: 'https://remote.test/' });
const calls = [];
dom.window.Capacitor = {
isNativePlatform: () => true,
isPluginAvailable: (name) => name === 'NativeDownload',
Plugins: {
NativeDownload: {
save: async (opts) => {
calls.push(opts);
return { path: `Download/${opts.name}` };
}
}
}
};
dom.window.WebSocket = class { constructor() { this.readyState = 0; } send() {} close() {} };
setGlobal('window', dom.window);
setGlobal('document', dom.window.document);
setGlobal('navigator', dom.window.navigator);
setGlobal('WebSocket', dom.window.WebSocket);
setGlobal('localStorage', dom.window.localStorage);
await import('../public/app.js?native-download');
const { openFilePreviewModal, saveCurrentActiveFile } = dom.window;
assert.equal(typeof saveCurrentActiveFile, 'function');
const contentBase64 = Buffer.from('conteudo de teste').toString('base64');
openFilePreviewModal({ ok: true, name: 'relatorio.txt', path: 'docs/relatorio.txt', mimeType: 'text/plain', size: 17, contentBase64 });
let blobClicked = false;
const originalCreate = dom.window.document.createElement.bind(dom.window.document);
dom.window.document.createElement = (tag) => {
const el = originalCreate(tag);
if (String(tag).toLowerCase() === 'a') el.click = () => { blobClicked = true; };
return el;
};
await saveCurrentActiveFile();
assert.equal(calls.length, 1, 'deve chamar NativeDownload.save uma vez');
assert.deepEqual(calls[0], { name: 'relatorio.txt', mimeType: 'text/plain', data: contentBase64 });
assert.equal(blobClicked, false, 'no app nativo não deve cair no link blob:, que a WebView ignora');
});
test('Pré-visualização: em APK antigo sem o plugin NativeDownload, "Baixar" avisa para atualizar o app em vez de falhar calado', async () => {
const dom = new JSDOM(html, { url: 'https://remote.test/' });
dom.window.Capacitor = {
isNativePlatform: () => true,
isPluginAvailable: () => false,
Plugins: {}
};
dom.window.WebSocket = class { constructor() { this.readyState = 0; } send() {} close() {} };
setGlobal('window', dom.window);
setGlobal('document', dom.window.document);
setGlobal('navigator', dom.window.navigator);
setGlobal('WebSocket', dom.window.WebSocket);
setGlobal('localStorage', dom.window.localStorage);
await import('../public/app.js?native-download-old-apk');
const { openFilePreviewModal, saveCurrentActiveFile } = dom.window;
openFilePreviewModal({ ok: true, name: 'a.txt', path: 'a.txt', mimeType: 'text/plain', size: 1, contentBase64: 'YQ==' });
let blobClicked = false;
const originalCreate = dom.window.document.createElement.bind(dom.window.document);
dom.window.document.createElement = (tag) => {
const el = originalCreate(tag);
if (String(tag).toLowerCase() === 'a') el.click = () => { blobClicked = true; };
return el;
};
await saveCurrentActiveFile();
assert.equal(blobClicked, false);
assert.match(dom.window.document.body.textContent, /Atualize o app Android/);
});