import { describe, expect, it, vi } from 'vitest'; import { AccountSuspensionService } from '../src/services/account-suspension.js'; import { RedditRateLimitError } from '../src/reddit/api.js'; import { createMockLogger } from './mockLogger.js'; const activeProfile = { createdAt: null, linkKarma: null, commentKarma: null, totalKarma: null, hasVerifiedEmail: null, isSuspended: false, }; describe('AccountSuspensionService', () => { it('pauses an authenticated account only when its own profile confirms suspension', async () => { const pause = vi.fn(); const reddit = { getAccountProfile: vi.fn().mockResolvedValue({ ...activeProfile, isSuspended: true }) }; const service = new AccountSuspensionService( 'owned-account', reddit, pause, createMockLogger(), { isFullyAuthenticated: () => true }, ); await expect(service.checkNow()).resolves.toBe(true); expect(reddit.getAccountProfile).toHaveBeenCalledWith('owned-account'); expect(pause).toHaveBeenCalledWith('Reddit profile marks this account as suspended'); }); it('does not pause for a missing profile, a transient failure, or a rate limit', async () => { const pause = vi.fn(); const reddit = { getAccountProfile: vi.fn() .mockResolvedValueOnce(null) .mockRejectedValueOnce(new Error('upstream unavailable')) .mockRejectedValueOnce(new RedditRateLimitError(1_000)) }; const service = new AccountSuspensionService( 'owned-account', reddit, pause, createMockLogger(), { isFullyAuthenticated: () => true }, ); await expect(service.checkNow()).resolves.toBe(false); await expect(service.checkNow()).resolves.toBe(false); await expect(service.checkNow()).resolves.toBe(false); expect(pause).not.toHaveBeenCalled(); }); it('does not probe while the account is not authenticated', async () => { const reddit = { getAccountProfile: vi.fn() }; const service = new AccountSuspensionService( 'owned-account', reddit, vi.fn(), createMockLogger(), { isFullyAuthenticated: () => false }, ); await expect(service.checkNow()).resolves.toBe(false); expect(reddit.getAccountProfile).not.toHaveBeenCalled(); }); });