// #region ColumnsTableTest [C:2] [TYPE Module] [SEMANTICS test, datasets, columns, component]
// @BRIEF Contract-focused unit tests for ColumnsTable.svelte component.
// @LAYER Test
import { describe, it, expect } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';

const COMPONENT_PATH = path.resolve(process.cwd(), 'src/routes/datasets/ColumnsTable.svelte');

describe('ColumnsTable Component', () => {
  it('component file exists', () => {
    expect(fs.existsSync(COMPONENT_PATH)).toBe(true);
  });

  it('bold verbose_name / italic column_name fallback', () => {
    const src = fs.readFileSync(COMPONENT_PATH, 'utf-8');
    expect(src).toContain('verbose_name');
    expect(src).toContain('font-medium');
    expect(src).toContain('italic');
    expect(src).toContain('name');
  });

  it('has type chip color classes', () => {
    const src = fs.readFileSync(COMPONENT_PATH, 'utf-8');
    expect(src).toContain('getTypeColor');
    expect(src).toContain('bg-green-100');
    expect(src).toContain('bg-purple-100');
    expect(src).toContain('bg-blue-100');
    expect(src).toContain('bg-orange-100');
  });

  it('has inline-edit state machine (editing/saving/error)', () => {
    const src = fs.readFileSync(COMPONENT_PATH, 'utf-8');
    expect(src).toContain('editingRowId');
    expect(src).toContain('editValue');
    expect(src).toContain('editError');
    expect(src).toContain('isSaving');
    expect(src).toContain('startEdit');
    expect(src).toContain('cancelEdit');
    expect(src).toContain('saveEdit');
  });

  it('textarea for editing with save/cancel buttons', () => {
    const src = fs.readFileSync(COMPONENT_PATH, 'utf-8');
    expect(src).toContain('textarea');
    expect(src).toContain('save_description');
    expect(src).toContain('maxlength');
  });

  it('shows error message with aria-live', () => {
    const src = fs.readFileSync(COMPONENT_PATH, 'utf-8');
    expect(src).toContain('aria-live');
    expect(src).toContain('editError');
  });

  it('shows empty state for 0 columns', () => {
    const src = fs.readFileSync(COMPONENT_PATH, 'utf-8');
    expect(src).toContain('columns.length === 0');
    expect(src).toContain('detail_empty');
  });

  it('calls PUT /datasets/{id}/columns/{col_id}/description', () => {
    const src = fs.readFileSync(COMPONENT_PATH, 'utf-8');
    expect(src).toContain('/columns/');
    expect(src).toContain('/description');
    expect(src).toContain('api.requestApi');
  });
});
