import * as fs from 'node:fs/promises'; import { resolvePromtoolPath } from './infrastructure-alert-settings.service'; jest.mock('node:fs/promises', () => ({ ...jest.requireActual('node:fs/promises'), access: jest.fn() })); describe('Prometheus binary resolution', () => { beforeEach(() => jest.resetAllMocks()); it('uses the official installer location when available', async () => { const check = jest.mocked(fs.access).mockResolvedValue(undefined); expect(await resolvePromtoolPath()).toBe('/usr/local/bin/promtool'); expect(check).toHaveBeenCalledTimes(1); }); it('supports the distribution package location', async () => { jest.mocked(fs.access).mockRejectedValueOnce(new Error('ENOENT')).mockResolvedValueOnce(undefined); expect(await resolvePromtoolPath()).toBe('/usr/bin/promtool'); }); it('fails rather than silently overriding an invalid explicitly configured binary', async () => { const check = jest.mocked(fs.access).mockRejectedValue(new Error('EACCES')); await expect(resolvePromtoolPath('/custom/promtool')).rejects.toThrow('promtool不可执行'); expect(check).toHaveBeenCalledTimes(1); }); });