import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import Fastify from 'fastify';
import { systemRoutes } from '../system-routes.js';
// Mock child_process
vi.mock('node:child_process', () => ({
    execFile: vi.fn((_cmd, args, _opts, cb) => {
        if (args[0] === 'view' && args[1] === 'expxagents') {
            cb(null, { stdout: '0.15.0\n' });
        }
        else {
            cb(null, { stdout: '' });
        }
    }),
}));
// Mock promisify to work with our mock
vi.mock('node:util', async () => {
    const actual = await vi.importActual('node:util');
    return {
        ...actual,
        promisify: (fn) => {
            return (...args) => {
                return new Promise((resolve, reject) => {
                    fn(...args, (err, result) => {
                        if (err)
                            reject(err);
                        else
                            resolve(result);
                    });
                });
            };
        },
    };
});
describe('system-routes', () => {
    let app;
    beforeEach(async () => {
        app = Fastify();
        app.decorate('requireAuth', async () => { });
        await app.register(systemRoutes, { packageVersion: '0.14.2' });
    });
    afterEach(async () => {
        await app.close();
    });
    it('GET /api/system/version should return current and latest versions', async () => {
        const res = await app.inject({ method: 'GET', url: '/api/system/version' });
        expect(res.statusCode).toBe(200);
        const body = res.json();
        expect(body.current).toBe('0.14.2');
        expect(body.latest).toBe('0.15.0');
        expect(body.updateAvailable).toBe(true);
    });
    it('GET /api/system/version should detect no update when current equals latest', async () => {
        await app.close();
        app = Fastify();
        app.decorate('requireAuth', async () => { });
        await app.register(systemRoutes, { packageVersion: '0.15.0' });
        const res = await app.inject({ method: 'GET', url: '/api/system/version' });
        const body = res.json();
        expect(body.updateAvailable).toBe(false);
    });
    it('POST /api/system/update should respond with ok when update available', async () => {
        const res = await app.inject({ method: 'POST', url: '/api/system/update' });
        expect(res.statusCode).toBe(200);
        const body = res.json();
        expect(body.ok).toBe(true);
        expect(body.message).toContain('0.15.0');
    });
    it('POST /api/system/update should respond with not ok when already up to date', async () => {
        await app.close();
        app = Fastify();
        app.decorate('requireAuth', async () => { });
        await app.register(systemRoutes, { packageVersion: '0.15.0' });
        const res = await app.inject({ method: 'POST', url: '/api/system/update' });
        const body = res.json();
        expect(body.ok).toBe(false);
        expect(body.message).toContain('up to date');
    });
});
//# sourceMappingURL=system-routes.test.js.map