|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { File } from 'node:buffer' |
| 3 | +import { act, createElement } from 'react' |
| 4 | +import { createRoot } from 'react-dom/client' |
| 5 | +import { describe, expect, it, vi } from 'vitest' |
| 6 | +import { useGitLabPermissionForm } from '@/connectors/gitlab/permission-config/use-permission-form' |
| 7 | + |
| 8 | +describe('GitLab permission file selection', () => { |
| 9 | + it('blocks an oversized combined upload and recovers when a file is replaced', async () => { |
| 10 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 11 | + const root = createRoot(document.createElement('div')) |
| 12 | + let form: ReturnType<typeof useGitLabPermissionForm> | undefined |
| 13 | + function Probe() { |
| 14 | + form = useGitLabPermissionForm() |
| 15 | + return null |
| 16 | + } |
| 17 | + const current = () => { |
| 18 | + if (!form) throw new Error('Permission form was not mounted') |
| 19 | + return form |
| 20 | + } |
| 21 | + act(() => root.render(createElement(Probe))) |
| 22 | + const csv = (content: string, filename: string) => |
| 23 | + Object.assign(new File([content], filename), { webkitRelativePath: '' }) |
| 24 | + try { |
| 25 | + act(() => current().setMode('csv')) |
| 26 | + const users = csv('1,a@example.test\n'.repeat(300_000), 'users.csv') |
| 27 | + const grants = csv('group/p,1\n'.repeat(490_000), 'projects.csv') |
| 28 | + expect(users.size + grants.size).toBeLessThan(10 * 1024 * 1024) |
| 29 | + await act(async () => { |
| 30 | + await current().selectFile('userMapping', users) |
| 31 | + await current().selectFile('projectPermissions', grants) |
| 32 | + }) |
| 33 | + expect(current().files.userMapping?.error).toBeUndefined() |
| 34 | + expect(current().files.projectPermissions?.error).toBeUndefined() |
| 35 | + expect(current().complete).toBe(false) |
| 36 | + await act(async () => { |
| 37 | + await current().selectFile('projectPermissions', csv('group/project,1', 'projects.csv')) |
| 38 | + }) |
| 39 | + expect(current().complete).toBe(true) |
| 40 | + act(() => current().reset()) |
| 41 | + expect(current().files).toEqual({}) |
| 42 | + } finally { |
| 43 | + act(() => root.unmount()) |
| 44 | + vi.unstubAllGlobals() |
| 45 | + } |
| 46 | + }) |
| 47 | +}) |
0 commit comments