import { describe, expect, it } from 'vitest';
import { hasWriteAll } from './permissions';

type Locals = { user?: { groups?: { permissions: string[] }[] } };

const localsWith = (permissions: string[][]): Locals => ({
	user: { groups: permissions.map((p) => ({ permissions: p })) }
});

describe('hasWriteAll', () => {
	it('returns true when a group grants WRITE_ALL', () => {
		expect(hasWriteAll(localsWith([['READ_ALL', 'WRITE_ALL']]))).toBe(true);
	});

	it('returns true when WRITE_ALL is in any of several groups', () => {
		expect(hasWriteAll(localsWith([['READ_ALL'], ['WRITE_ALL']]))).toBe(true);
	});

	it('returns false when no group grants WRITE_ALL', () => {
		expect(hasWriteAll(localsWith([['READ_ALL'], ['ANNOTATE_ALL']]))).toBe(false);
	});

	it('returns false for an anonymous user (no locals.user)', () => {
		expect(hasWriteAll({})).toBe(false);
	});

	it('returns false when the user has no groups', () => {
		expect(hasWriteAll({ user: {} })).toBe(false);
	});
});
