/**
 * WORM Storage Tests
 */

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { WORMStorage } from './worm';
import { StorageClient } from './storage';

vi.mock('./storage');

describe('WORMStorage', () => {
  let storage: WORMStorage;
  const config = {
    provider: 's3' as const,
    bucket: 'test-bucket',
    region: 'us-east-1',
  };

  beforeEach(() => {
    storage = new WORMStorage(config);
    vi.clearAllMocks();
  });

  describe('upload', () => {
    it('should upload object if it does not exist', async () => {
      const object = {
        key: 'test-key',
        content: Buffer.from('test content'),
      };

      vi.spyOn(storage, 'objectExists').mockResolvedValueOnce(false);
      vi.spyOn(StorageClient.prototype, 'upload').mockResolvedValueOnce(object.key);

      const result = await storage.upload(object);

      expect(result).toBe(object.key);
    });

    it('should throw error if object already exists', async () => {
      const object = {
        key: 'existing-key',
        content: Buffer.from('test content'),
      };

      vi.spyOn(storage, 'objectExists').mockResolvedValueOnce(true);

      await expect(storage.upload(object)).rejects.toThrow(
        'Object existing-key already exists in WORM storage'
      );
    });
  });

  describe('delete', () => {
    it('should throw error when trying to delete', async () => {
      const key = 'test-key';

      await expect(storage.delete(key)).rejects.toThrow(
        'Deletion not allowed in WORM mode'
      );
    });
  });
});

