import { afterEach, beforeEach, describe, expect, spyOn, it } from 'bun:test' import * as fs from 'node:fs' import { InstagramCredentialManager } from '@/platforms/instagram/credential-manager' import { ensureInstagramAuth } from '@/platforms/instagram/ensure-auth' import type { InstagramAccount } from '@/platforms/instagram/types' let getAccountSpy: ReturnType let getAccountPathsSpy: ReturnType let existsSyncSpy: ReturnType let exitSpy: ReturnType let consoleSpy: ReturnType const validAccount: InstagramAccount = { account_id: 'test-account', username: 'testuser', created_at: new Date().toISOString(), updated_at: new Date().toISOString(), } const validPaths = { account_dir: '/tmp/test/instagram/test-account', session_path: '/tmp/test/instagram/test-account/session.json', } beforeEach(() => { getAccountSpy = spyOn(InstagramCredentialManager.prototype, 'getAccount').mockResolvedValue(null) getAccountPathsSpy = spyOn(InstagramCredentialManager.prototype, 'getAccountPaths').mockReturnValue(validPaths) existsSyncSpy = spyOn(fs, 'existsSync').mockReturnValue(false) exitSpy = spyOn(process, 'exit').mockImplementation((code) => { throw new Error(`process.exit(${code})`) }) consoleSpy = spyOn(console, 'log').mockImplementation(() => {}) }) afterEach(() => { getAccountSpy?.mockRestore() getAccountPathsSpy?.mockRestore() existsSyncSpy?.mockRestore() exitSpy?.mockRestore() consoleSpy?.mockRestore() }) describe('ensureInstagramAuth', () => { it('exits with error when no account configured', async () => { // given getAccountSpy.mockResolvedValue(null) // when / then await expect(ensureInstagramAuth()).rejects.toThrow('process.exit(1)') expect(exitSpy).toHaveBeenCalledWith(1) expect(consoleSpy).toHaveBeenCalled() }) it('exits with error when session file missing', async () => { // given getAccountSpy.mockResolvedValue(validAccount) existsSyncSpy.mockReturnValue(false) // when / then await expect(ensureInstagramAuth()).rejects.toThrow('process.exit(1)') expect(exitSpy).toHaveBeenCalledWith(1) expect(consoleSpy).toHaveBeenCalled() }) it('succeeds when account and session file exist', async () => { // given getAccountSpy.mockResolvedValue(validAccount) existsSyncSpy.mockReturnValue(true) // when await ensureInstagramAuth() // then expect(exitSpy).not.toHaveBeenCalled() }) })