/** Lot E: the display rules of the draft's mode (the server decides every write). */ import { describe, expect, it } from 'vitest' import { draftEditable, editorMode, freezingRound, hasEditRight, modeAfterSave, storeCanEdit, } from '@/editor/mode' import { tabsFor } from '@/editor/tabs' const sheet = (patch: Record = {}) => ({ status: 'active', historical: false, draft_version: { version_id: 'v1', version_number: 1, revision: 1 }, rounds: [], ...patch, }) as never const round = (status: string, version_id = 'v1') => ({ version_id, status }) as never describe('editorMode', () => { const base = { hasRight: true, sheet: sheet(), wide: true } it('edit, narrow and the reasons that read only, strongest first', () => { expect(editorMode(base)).toBe('edit') expect(editorMode({ ...base, wide: false })).toBe('narrow') expect(editorMode({ ...base, hasRight: false, wide: false })).toBe('rights') expect( editorMode({ ...base, sheet: sheet({ status: 'archived' }), wide: false, }), ).toBe('inactive') expect(editorMode({ ...base, sheet: sheet({ historical: true }) })).toBe( 'historical', ) expect( editorMode({ ...base, sheet: sheet({ rounds: [round('open')] }) }), ).toBe('frozen') }) it('only verifying, open and approved rounds of the edited draft freeze it', () => { for (const status of ['verifying', 'open', 'approved']) { expect(freezingRound(sheet({ rounds: [round(status)] }))).not.toBeNull() } for (const status of [ 'changes_requested', 'withdrawn', 'verification_failed', ]) { expect(freezingRound(sheet({ rounds: [round(status)] }))).toBeNull() } expect( freezingRound(sheet({ rounds: [round('open', 'other')] })), ).toBeNull() }) it('the right needs the capability and the admin or owner role', () => { expect(hasEditRight(true, ['admin'])).toBe(true) expect(hasEditRight(true, ['owner'])).toBe(true) expect(hasEditRight(true, ['support'])).toBe(false) expect(hasEditRight(false, ['admin'])).toBe(false) }) it('the overview offers « Ouvrir » only when the draft can be edited', () => { expect(draftEditable(true, sheet())).toBe(true) expect(draftEditable(true, sheet({ rounds: [round('verifying')] }))).toBe( false, ) expect(draftEditable(false, sheet())).toBe(false) }) it('the store may use a recovery copy where the account could save', () => { expect(['edit', 'narrow'].every((m) => storeCanEdit(m as never))).toBe(true) expect( ['rights', 'frozen', 'historical', 'inactive'].some((m) => storeCanEdit(m as never), ), ).toBe(false) }) }) describe('modeAfterSave', () => { it('a refused save names the state the page missed', () => { expect(modeAfterSave('edit', 'draft.frozen')).toBe('frozen') expect(modeAfterSave('edit', 'sheet.inactive')).toBe('inactive') expect(modeAfterSave('edit', 'rights.mfa')).toBe('rights') expect(modeAfterSave('edit', 'network')).toBe('edit') expect(modeAfterSave('edit', null)).toBe('edit') expect(modeAfterSave('narrow', 'draft.frozen')).toBe('narrow') }) }) describe('tabsFor', () => { it('the static lists of board 05, four tabs', () => { expect(tabsFor('wide').more).toEqual([]) expect(tabsFor('medium').shown).toEqual([ 'general', 'annotations', 'findings', 'verification', ]) expect(tabsFor('small').shown).toEqual(['annotations', 'verification']) expect(tabsFor('small').more).toEqual(['general', 'findings']) }) })