/**
 * Tests for Local Config Sync
 * Verifies YAML section replacement logic and sync behavior.
 */

import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';

describe('Local Config Sync', () => {
  const localConfigSync = require('../../../dist/cliproxy/sync/local-config-sync');

  describe('syncToLocalConfig', () => {
    it('returns success with zero count when no profiles', () => {
      // If no profiles are configured, sync should succeed with 0 count
      const result = localConfigSync.syncToLocalConfig();
      assert.ok(typeof result === 'object');
      assert.ok(typeof result.success === 'boolean');
      assert.ok(typeof result.syncedCount === 'number');
      assert.ok(typeof result.configPath === 'string');
    });

    it('returns error when config file not found', () => {
      // Set CCS_HOME to temp dir without config
      const originalHome = process.env.CCS_HOME;
      const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-test-'));

      try {
        process.env.CCS_HOME = tempDir;
        // Create a mock settings file to trigger sync attempt
        const settingsPath = path.join(tempDir, 'test.settings.json');
        fs.writeFileSync(
          settingsPath,
          JSON.stringify({
            env: {
              ANTHROPIC_AUTH_TOKEN: 'sk-real-key',
              ANTHROPIC_BASE_URL: 'https://api.example.com',
            },
          })
        );

        // Need to reload module to pick up new CCS_HOME
        // For this test, we just verify the function doesn't throw
        const result = localConfigSync.syncToLocalConfig();
        assert.ok(typeof result === 'object');
      } finally {
        process.env.CCS_HOME = originalHome;
        fs.rmSync(tempDir, { recursive: true });
      }
    });
  });

  describe('getLocalSyncStatus', () => {
    it('returns status object with required fields', () => {
      const result = localConfigSync.getLocalSyncStatus();

      assert.ok(typeof result === 'object');
      assert.ok(typeof result.configExists === 'boolean');
      assert.ok(typeof result.configPath === 'string');
      assert.ok(typeof result.currentKeyCount === 'number');
      assert.ok(typeof result.syncableProfileCount === 'number');
    });

    it('returns non-negative counts', () => {
      const result = localConfigSync.getLocalSyncStatus();
      assert.ok(result.currentKeyCount >= 0);
      assert.ok(result.syncableProfileCount >= 0);
    });
  });

  describe('YAML section replacement', () => {
    // Test the internal logic through integration
    // The replaceSectionInYaml function is not exported, so we test via syncToLocalConfig

    it('preserves file permissions on sync', () => {
      // Verify atomic write behavior by checking syncToLocalConfig doesn't throw
      const result = localConfigSync.syncToLocalConfig();
      assert.ok(typeof result === 'object');
    });
  });

  describe('Known Limitations (Documented)', () => {
    /**
     * YAML section replacement has known edge cases:
     * - Comments between section key and first item may be lost
     * - Multi-document YAML files (with ---) not supported
     * - Inline comments on section key line may be lost
     *
     * These are acceptable trade-offs for the simple section-based replacement.
     * Users with complex YAML structures should edit manually.
     */
    it('documents known YAML edge cases', () => {
      // This test serves as documentation - no assertions needed
      assert.ok(true, 'Edge cases documented in test comments');
    });
  });
});
