import { describe, expect, it } from 'vitest';

import { allowsFileAction } from './fileAccess';

describe('upload file action gates', () => {
  it('preserves legacy controls when no runtime permissions are supplied', () => {
    expect(allowsFileAction(undefined, 'DOWNLOAD')).toBe(true);
  });
  it('fails closed while loading or after a failed refresh', () => {
    expect(allowsFileAction({ status: 'loading', actions: { DOWNLOAD: true } }, 'DOWNLOAD')).toBe(false);
    expect(allowsFileAction({ status: 'error', actions: { DOWNLOAD: true } }, 'DOWNLOAD')).toBe(false);
  });
  it('separates viewing, downloading and editing', () => {
    const access = { status: 'ready' as const, actions: { VIEW: true, DOWNLOAD: false, EDIT: true } };
    expect(allowsFileAction(access, 'VIEW')).toBe(true);
    expect(allowsFileAction(access, 'EDIT')).toBe(true);
    expect(allowsFileAction(access, 'DOWNLOAD')).toBe(false);
    expect(allowsFileAction(access, 'REMOVE')).toBe(false);
  });
  it('does not interpret malformed values as grants', () => {
    expect(allowsFileAction({ status: 'ready', actions: { DOWNLOAD: 'true' } } as any, 'DOWNLOAD')).toBe(false);
    expect(allowsFileAction(null as any, 'VIEW')).toBe(false);
  });
});
