135 lines
5.2 KiB
JavaScript
135 lines
5.2 KiB
JavaScript
import { describe, it } from 'node:test';
|
|
import assert from 'node:assert';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import crypto from 'node:crypto';
|
|
import { AgentUpdater, extractZipNative } from '../src/gateway/agent-updater.js';
|
|
|
|
describe('AgentUpdater — Auto-Atualização do Daemon Gateway', () => {
|
|
const testDir = path.resolve('.temp_test_updater');
|
|
|
|
it('deve inicializar com diretório e persistir/recuperar hash de estado', () => {
|
|
if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
|
|
fs.mkdirSync(testDir, { recursive: true });
|
|
|
|
const updater = new AgentUpdater({ rootDir: testDir, publicHost: 'https://example.com' });
|
|
assert.strictEqual(updater.getLocalHash(), null);
|
|
|
|
const fakeHash = crypto.createHash('sha256').update('maestri-agent-v1').digest('hex');
|
|
updater.saveLocalHash(fakeHash, '1.0.1');
|
|
|
|
assert.strictEqual(updater.getLocalHash(), fakeHash);
|
|
|
|
fs.rmSync(testDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it('deve identificar quando não há atualização necessária', async () => {
|
|
if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
|
|
fs.mkdirSync(testDir, { recursive: true });
|
|
|
|
const updater = new AgentUpdater({ rootDir: testDir, publicHost: 'https://example.com' });
|
|
const currentHash = 'abc123hash';
|
|
updater.saveLocalHash(currentHash, '1.0.1');
|
|
|
|
// Mock checkRemoteVersion retornando a mesma versão
|
|
updater.checkRemoteVersion = async () => ({
|
|
version: '1.0.1',
|
|
sha256: currentHash,
|
|
downloadUrl: '/maestri-agent.zip'
|
|
});
|
|
|
|
const updated = await updater.checkAndApply();
|
|
assert.strictEqual(updated, false);
|
|
|
|
fs.rmSync(testDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it('deve disparar performUpdate na primeira execução quando localHash é nulo', async () => {
|
|
if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
|
|
fs.mkdirSync(testDir, { recursive: true });
|
|
|
|
const updater = new AgentUpdater({ rootDir: testDir, publicHost: 'https://example.com' });
|
|
let performed = false;
|
|
updater.performUpdate = async (remote) => {
|
|
performed = true;
|
|
updater.saveLocalHash(remote.sha256, remote.version);
|
|
return true;
|
|
};
|
|
|
|
updater.checkRemoteVersion = async () => ({
|
|
version: '1.0.5',
|
|
sha256: 'initial_hash_123',
|
|
downloadUrl: '/maestri-agent.zip'
|
|
});
|
|
|
|
const updated = await updater.checkAndApply();
|
|
assert.strictEqual(updated, true);
|
|
assert.strictEqual(performed, true);
|
|
assert.strictEqual(updater.getLocalHash(), 'initial_hash_123');
|
|
|
|
fs.rmSync(testDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it('deve disparar performUpdate quando o hash é diferente mesmo na mesma versão semântica', async () => {
|
|
if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
|
|
fs.mkdirSync(testDir, { recursive: true });
|
|
|
|
const updater = new AgentUpdater({ rootDir: testDir, publicHost: 'https://example.com' });
|
|
updater.saveLocalHash('old_hash_111', '1.0.5');
|
|
|
|
let performed = false;
|
|
updater.performUpdate = async (remote) => {
|
|
performed = true;
|
|
updater.saveLocalHash(remote.sha256, remote.version);
|
|
return true;
|
|
};
|
|
|
|
updater.checkRemoteVersion = async () => ({
|
|
version: '1.0.5', // Mesma versão!
|
|
sha256: 'new_hash_222', // Hash diferente!
|
|
downloadUrl: '/maestri-agent.zip'
|
|
});
|
|
|
|
const updated = await updater.checkAndApply();
|
|
assert.strictEqual(updated, true);
|
|
assert.strictEqual(performed, true);
|
|
assert.strictEqual(updater.getLocalHash(), 'new_hash_222');
|
|
|
|
fs.rmSync(testDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it('deve descompactar ZIP com sucesso usando o extrator nativo (extractZipNative)', () => {
|
|
const zipPath = path.resolve('public', 'maestri-agent.zip');
|
|
if (!fs.existsSync(zipPath)) {
|
|
return; // Pula se o pacote ainda não foi compilado
|
|
}
|
|
const extractDir = path.resolve('.temp_test_extract_native');
|
|
if (fs.existsSync(extractDir)) fs.rmSync(extractDir, { recursive: true, force: true });
|
|
fs.mkdirSync(extractDir, { recursive: true });
|
|
|
|
const count = extractZipNative(zipPath, extractDir);
|
|
assert.ok(count > 0, 'Deve ter extraído ao menos um arquivo');
|
|
assert.ok(fs.existsSync(path.join(extractDir, 'bin', 'maestri-remote.js')), 'bin/maestri-remote.js deve existir');
|
|
assert.ok(fs.existsSync(path.join(extractDir, 'package.json')), 'package.json deve existir');
|
|
|
|
fs.rmSync(extractDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it('deve executar extractZip através da classe com fallback resiliente', () => {
|
|
const zipPath = path.resolve('public', 'maestri-agent.zip');
|
|
if (!fs.existsSync(zipPath)) {
|
|
return;
|
|
}
|
|
const extractDir = path.resolve('.temp_test_extract_method');
|
|
if (fs.existsSync(extractDir)) fs.rmSync(extractDir, { recursive: true, force: true });
|
|
fs.mkdirSync(extractDir, { recursive: true });
|
|
|
|
const updater = new AgentUpdater({ rootDir: testDir });
|
|
updater.extractZip(zipPath, extractDir);
|
|
|
|
assert.ok(fs.existsSync(path.join(extractDir, 'bin', 'maestri-remote.js')), 'bin/maestri-remote.js deve existir após extractZip');
|
|
|
|
fs.rmSync(extractDir, { recursive: true, force: true });
|
|
});
|
|
});
|