import test from 'node:test'; import assert from 'node:assert/strict'; import { JSDOM } from 'jsdom'; const 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/); });