import { promises as fsPromises } from 'fs'; import netrc from 'node-netrc'; import { convertNetrcToIni } from './convertNetrcToIni'; import { getCredentials, setCredentials, getCredentialsFilePath, } from './state/credentials'; jest.mock('fs', () => { const real = jest.requireActual('fs'); return { ...real, promises: { ...real.promises, access: jest.fn(), rm: jest.fn(), readFile: jest.fn(), }, }; }); jest.mock('node-netrc', () => { const fn: any = jest.fn(); fn.update = jest.fn(); return { __esModule: true, default: fn }; }); jest.mock('./state/credentials'); jest.mock('./log'); const { access, rm, readFile } = fsPromises as unknown as { access: jest.Mock; rm: jest.Mock; readFile: jest.Mock; }; // AC-7.1: the legacy `.netrc` `alkscli` password entry must NOT be migrated, // while the `alksclitoken` token entry still migrates to refresh_token. describe('convertNetrcToIni (AC-7.1)', () => { beforeEach(() => { jest.clearAllMocks(); (getCredentialsFilePath as jest.Mock).mockReturnValue( '/tmp/fake-credentials' ); // .netrc exists; credentials file does not → conversion runs access.mockImplementation((p: string) => String(p).endsWith('.netrc') ? Promise.resolve() : Promise.reject(new Error('ENOENT')) ); (getCredentials as jest.Mock).mockResolvedValue({}); (setCredentials as jest.Mock).mockResolvedValue(undefined); readFile.mockResolvedValue(''); rm.mockResolvedValue(undefined); (netrc as unknown as jest.Mock).mockImplementation((key: string) => { if (key === 'alksclitoken') { return { password: 'tok-abc' }; } if (key === 'alkscli') { return { password: 'net-pw' }; } return {}; }); }); it('migrates the alksclitoken token entry to refresh_token', async () => { await convertNetrcToIni(); expect(setCredentials).toHaveBeenCalledTimes(1); const written = (setCredentials as jest.Mock).mock.calls[0][0]; expect(written.refresh_token).toBe('tok-abc'); }); it('does not migrate the legacy alkscli password entry', async () => { await convertNetrcToIni(); const written = (setCredentials as jest.Mock).mock.calls[0][0]; expect(written.password).toBeUndefined(); // the password entry is never even read expect(netrc).not.toHaveBeenCalledWith('alkscli'); }); });