import { assertEquals, assertExists } from 'jsr:@std/assert';
import { afterAll, beforeAll, describe, it } from 'jsr:@std/testing/bdd';
import { createTestContainer, getTestPorts } from './helpers/server.helper.ts';
import { ObjectStorageContainer } from '../ts/index.ts';
import { defaultConfig } from '../ts/types.ts';

const PORT_INDEX = 0;
const ports = getTestPorts(PORT_INDEX);

describe('ObjectStorageContainer lifecycle', { sanitizeResources: false, sanitizeOps: false }, () => {
  let container: ObjectStorageContainer;

  it('should create container with default config values', () => {
    const c = new ObjectStorageContainer();
    assertEquals(c.config.objstPort, defaultConfig.objstPort);
    assertEquals(c.config.uiPort, defaultConfig.uiPort);
    assertEquals(c.config.region, defaultConfig.region);
    assertEquals(c.config.adminPassword, defaultConfig.adminPassword);
  });

  it('should create container with custom config overrides', () => {
    container = createTestContainer(PORT_INDEX);
    assertEquals(container.config.objstPort, ports.objstPort);
    assertEquals(container.config.uiPort, ports.uiPort);
    assertEquals(container.config.adminPassword, 'testpassword');
    assertEquals(container.config.accessCredentials[0].accessKeyId, 'testkey');
  });

  it('should start successfully', async () => {
    await container.start();
    assertEquals(container.startedAt > 0, true);
  });

  it('should have s3Client initialized after start', () => {
    assertExists(container.s3Client);
  });

  it('should have smartstorageInstance initialized after start', () => {
    assertExists(container.smartstorageInstance);
  });

  it('should have policyManager with empty policies', () => {
    assertExists(container.policyManager);
    const policies = container.policyManager.listPolicies();
    assertEquals(policies.length, 0);
  });

  it('should have opsServer started', () => {
    assertExists(container.opsServer);
    assertExists(container.opsServer.server);
  });

  it('should stop cleanly', async () => {
    await container.stop();
  });
});
