|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('@quenty/cli-output-helpers', () => ({ |
| 4 | + OutputHelper: { |
| 5 | + error: vi.fn(), |
| 6 | + warn: vi.fn(), |
| 7 | + info: vi.fn(), |
| 8 | + verbose: vi.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +import { validateCookieAsync } from './index.js'; |
| 13 | +import { OutputHelper } from '@quenty/cli-output-helpers'; |
| 14 | + |
| 15 | +const mockedOutputHelper = vi.mocked(OutputHelper); |
| 16 | + |
| 17 | +describe('validateCookieAsync', () => { |
| 18 | + const originalFetch = globalThis.fetch; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + vi.restoreAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + globalThis.fetch = originalFetch; |
| 26 | + }); |
| 27 | + |
| 28 | + it('continues without error when cookie is valid (HTTP 200)', async () => { |
| 29 | + const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never); |
| 30 | + globalThis.fetch = vi.fn().mockResolvedValue({ status: 200 }); |
| 31 | + |
| 32 | + await validateCookieAsync('valid-cookie'); |
| 33 | + |
| 34 | + expect(exitSpy).not.toHaveBeenCalled(); |
| 35 | + expect(mockedOutputHelper.error).not.toHaveBeenCalled(); |
| 36 | + expect(mockedOutputHelper.warn).not.toHaveBeenCalled(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('exits with error when cookie is invalid (HTTP 401)', async () => { |
| 40 | + const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never); |
| 41 | + globalThis.fetch = vi.fn().mockResolvedValue({ status: 401 }); |
| 42 | + |
| 43 | + await validateCookieAsync('expired-cookie'); |
| 44 | + |
| 45 | + expect(mockedOutputHelper.error).toHaveBeenCalledWith( |
| 46 | + 'ROBLOSECURITY cookie is invalid or expired (HTTP 401). Update the cookie and try again.', |
| 47 | + ); |
| 48 | + expect(exitSpy).toHaveBeenCalledWith(1); |
| 49 | + }); |
| 50 | + |
| 51 | + it('continues with warning when fetch throws (network error)', async () => { |
| 52 | + const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never); |
| 53 | + globalThis.fetch = vi.fn().mockRejectedValue(new Error('network error')); |
| 54 | + |
| 55 | + await validateCookieAsync('some-cookie'); |
| 56 | + |
| 57 | + expect(mockedOutputHelper.warn).toHaveBeenCalledWith( |
| 58 | + 'Could not validate ROBLOSECURITY cookie (network error). Continuing anyway.', |
| 59 | + ); |
| 60 | + expect(exitSpy).not.toHaveBeenCalled(); |
| 61 | + expect(mockedOutputHelper.error).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments