Files
makelore/tests/unit/teacher-catalog-revision.test.ts

35 lines
1.5 KiB
TypeScript

import { beforeEach, expect, it, vi } from 'vitest';
const mock = vi.hoisted(() => ({
binding: vi.fn(), current: vi.fn(), token: vi.fn(), fetch: vi.fn(),
}));
vi.mock('../../electron/services/works-square-session', () => ({
getWorksSquareAccountBinding: mock.binding,
isCurrentWorksSquareAccountBinding: mock.current,
getValidWorksSquareAccessToken: mock.token,
}));
vi.mock('../../electron/utils/proxy-fetch', () => ({ proxyAwareFetch: mock.fetch }));
import { teacherCatalogRevision } from '../../electron/coding-teacher/config-client';
beforeEach(() => {
vi.resetAllMocks();
mock.binding.mockReturnValue({ accountId: 'student', generation: 1 });
mock.current.mockReturnValue(true);
mock.token.mockResolvedValue('student-token');
mock.fetch.mockResolvedValue(new Response(JSON.stringify({ revision: 12 })));
});
it('reads only the small directory revision with current account authentication', async () => {
expect(await teacherCatalogRevision()).toEqual({ revision: 12 });
expect(mock.fetch).toHaveBeenCalledTimes(1);
expect(mock.fetch.mock.calls[0][0]).toMatch(/\/api\/coding-teacher\/teachers\/revision$/);
expect(mock.fetch.mock.calls[0][1].headers.Authorization).toBe('Bearer student-token');
});
it('discards a response when the account changes while waiting', async () => {
mock.fetch.mockImplementation(async () => {
mock.current.mockReturnValue(false);
return new Response(JSON.stringify({ revision: 99 }));
});
await expect(teacherCatalogRevision()).rejects.toMatchObject({ code: 'teacher_account_changed' });
});