import { createElement } from 'react' import { afterAll, beforeAll, describe, expect, it } from 'vitest' import { cleanup, render, screen } from '@testing-library/react' import type { CatalogGrant } from '@faunosteo/supabase' import GrantList from '@/components/admin/GrantList' import { useLocaleStore } from '@/state/localeStore' import { parseGrant } from '@/admin/validation' const GRANT = { kind: 'catalog_reader' as const, reason: ' Lecture ', verification_note: '', } const EXPIRING: CatalogGrant = { id: 'g-1', user_id: 'user-2', display_name: 'Paul', email: 'paul@example.test', kind: 'catalog_reader', reason: 'Essai', verification_note: null, granted_by: 'user-1', granted_by_name: 'Alice', granted_at: '2026-11-01T10:00:00Z', expires_at: '2026-12-31T22:59:59.999Z', revoked_at: null, revoked_by: null, revocation_reason: null, state: 'active', effective: true, } // Zones on both sides of Paris: the expiry never follows the device. describe.each(['America/Los_Angeles', 'Pacific/Kiritimati'])( 'grant expiry under the device zone %s', (zone) => { const previous = process.env.TZ beforeAll(() => { process.env.TZ = zone expect(Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(zone) }) afterAll(() => { if (previous === undefined) delete process.env.TZ else process.env.TZ = previous }) it('the reader needs no note; the expiry is the end of the Paris day', () => { expect( parseGrant({ ...GRANT, expires_on: '2026-12-31' }, '2026-11-02'), ).toEqual({ ok: true, value: { kind: 'catalog_reader', reason: 'Lecture', expires_at: '2026-12-31T22:59:59.999Z', }, }) expect( parseGrant({ ...GRANT, expires_on: '2026-07-15' }, '2026-07-01'), ).toMatchObject({ ok: true, value: { expires_at: '2026-07-15T21:59:59.999Z' }, }) }) it('refuses today, accepts tomorrow', () => { expect( parseGrant({ ...GRANT, expires_on: '2026-11-02' }, '2026-11-02'), ).toMatchObject({ ok: false, errors: { expires_on: 'admin.access.error.expiry' }, }) expect( parseGrant({ ...GRANT, expires_on: '2026-11-03' }, '2026-11-02'), ).toMatchObject({ ok: true }) }) it('refuses a day that does not exist', () => { expect( parseGrant({ ...GRANT, expires_on: '2026-02-30' }, '2026-01-01'), ).toMatchObject({ ok: false, errors: { expires_on: 'admin.access.error.expiry' }, }) }) it('the grant list shows the Paris expiry day', () => { // 23:59:59.999 Paris on 31 December; the device day would be 1 January east of Paris. useLocaleStore.setState({ locale: 'fr' }) render( createElement(GrantList, { grants: [EXPIRING], onChanged: () => undefined, }), ) expect(screen.getByText(/31 décembre 2026/)).toBeInTheDocument() expect(screen.queryByText(/1 janvier 2027/)).toBeNull() cleanup() }) }, )