import { ConflictException } from '@nestjs/common';
import { APP_DATA_KEYS } from '../data/data-keys';
import {
  APP_DATA_RETIREMENT_CONFIG,
  AppDataRetirementService,
} from './app-data-retirement.service';

describe('AppDataRetirementService', () => {
  const service = new AppDataRetirementService();

  it('declares a retirement state for every allowed AppData key', () => {
    expect(Object.keys(APP_DATA_RETIREMENT_CONFIG).sort()).toEqual([...APP_DATA_KEYS].sort());
  });

  it('rejects writes to frozen business documents with replacement guidance', () => {
    expect(() => service.assertWritable('dev-tasks')).toThrow(ConflictException);

    try {
      service.assertWritable('dev-tasks');
    } catch (error: any) {
      expect(error.getResponse()).toMatchObject({
        code: 'APP_DATA_WRITE_FROZEN',
        key: 'dev-tasks',
        state: 'write_frozen',
        replacement: '/api/v1/versions/:versionId/dev-tasks',
      });
    }
  });

  it('treats archived documents as read-only', () => {
    try {
      service.assertWritable('xiaobao-risk-snapshots');
    } catch (error: any) {
      expect(error.getResponse()).toMatchObject({
        code: 'APP_DATA_WRITE_FROZEN',
        key: 'xiaobao-risk-snapshots',
        state: 'read_only_archive',
      });
    }
  });
});
