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

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

describe('Inline Edit', () => {
  it('click opens textarea with prefill (columns)', () => {
    const src = fs.readFileSync(COLUMNS_PATH, 'utf-8');
    expect(src).toContain('textarea');
    expect(src).toContain('editValue = col.description');
    expect(src).toContain('startEdit');
  });

  it('save calls API and updates description (columns)', () => {
    const src = fs.readFileSync(COLUMNS_PATH, 'utf-8');
    expect(src).toContain('PUT');
    expect(src).toContain('/description');
    expect(src).toContain('col.description = result.description');
  });

  it('cancel reverts to original (columns)', () => {
    const src = fs.readFileSync(COLUMNS_PATH, 'utf-8');
    expect(src).toContain('cancelEdit');
    expect(src).toContain('editingRowId = null');
  });

  it('save error keeps textarea open (columns)', () => {
    const src = fs.readFileSync(COLUMNS_PATH, 'utf-8');
    expect(src).toContain('editError');
    expect(src).toContain('catch');
    expect(src).toContain('isSaving = false');
  });

  it('save error keeps textarea open (metrics)', () => {
    const src = fs.readFileSync(METRICS_PATH, 'utf-8');
    expect(src).toContain('editError');
    expect(src).toContain('catch');
    expect(src).toContain('isSaving = false');
  });

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

  it('shows expression as hint when no description (metrics)', () => {
    const src = fs.readFileSync(METRICS_PATH, 'utf-8');
    expect(src).toContain('expression');
    expect(src).toContain('font-mono');
  });
});
