// [DEF:frontend.src.lib.components.assistant.__tests__.assistant_chat_integration:Module]
// @TIER: STANDARD
// @SEMANTICS: assistant, integration-test, ux-contract, i18n
// @PURPOSE: Contract-level integration checks for assistant chat panel implementation and localization wiring.
// @LAYER: UI Tests
// @RELATION: VERIFIES -> frontend/src/lib/components/assistant/AssistantChatPanel.svelte
// @INVARIANT: Critical assistant UX states and action hooks remain present in component source.

import { describe, it, expect } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';

const COMPONENT_PATH = path.resolve(
  process.cwd(),
  'src/lib/components/assistant/AssistantChatPanel.svelte',
);
const EN_LOCALE_PATH = path.resolve(
  process.cwd(),
  'src/lib/i18n/locales/en.json',
);
const RU_LOCALE_PATH = path.resolve(
  process.cwd(),
  'src/lib/i18n/locales/ru.json',
);

// [DEF:readJson:Function]
// @PURPOSE: Read and parse JSON fixture file from disk.
// @PRE: filePath points to existing UTF-8 JSON file.
// @POST: Returns parsed object representation.
function readJson(filePath) {
  return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
}
// [/DEF:readJson:Function]

// [DEF:assistant_chat_contract_tests:Function]
// @PURPOSE: Validate assistant chat component contract and locale integration without DOM runtime dependency.
// @PRE: Component and locale files exist in expected paths.
// @POST: Contract checks guarantee assistant UI anchors and i18n wiring remain intact.
describe('AssistantChatPanel integration contract', () => {
  it('contains semantic anchors and UX contract tags', () => {
    const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');

    expect(source).toContain('<!-- [DEF' + ':AssistantChatPanel:Component] -->');
    expect(source).toContain('@TIER: CRITICAL');
    expect(source).toContain('@UX_STATE: LoadingHistory');
    expect(source).toContain('@UX_STATE: Sending');
    expect(source).toContain('@UX_STATE: Error');
    expect(source).toContain('@UX_FEEDBACK: Started operation surfaces task_id');
    expect(source).toContain('@UX_RECOVERY: User can retry command');
    expect(source).toContain('<!-- [/DEF' + ':AssistantChatPanel:Component] -->');
  });

  it('keeps confirmation/task-tracking action hooks in place', () => {
    const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');

    expect(source).toContain('if (action.type === "confirm" && message.confirmation_id)');
    expect(source).toContain('if (action.type === "cancel" && message.confirmation_id)');
    expect(source).toContain('if (action.type === "open_task" && action.target)');
    expect(source).toContain('openDrawerForTask(action.target)');
    expect(source).toContain('goto("/reports")');
  });

  it('uses i18n bindings for assistant UI labels', () => {
    const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');

    expect(source).toContain('$t.assistant?.title');
    expect(source).toContain('$t.assistant?.input_placeholder');
    expect(source).toContain('$t.assistant?.send');
    expect(source).toContain('$t.assistant?.states?.[message.state]');
    expect(source).toContain('$t.assistant?.open_task_drawer');
  });

  it('provides assistant locale keys in both en and ru dictionaries', () => {
    const en = readJson(EN_LOCALE_PATH);
    const ru = readJson(RU_LOCALE_PATH);

    expect(en.assistant.title).toBeTruthy();
    expect(en.assistant.send).toBeTruthy();
    expect(en.assistant.states.needs_confirmation).toBeTruthy();

    expect(ru.assistant.title).toBeTruthy();
    expect(ru.assistant.send).toBeTruthy();
    expect(ru.assistant.states.needs_confirmation).toBeTruthy();
  });
});
// [/DEF:assistant_chat_contract_tests:Function]
// [/DEF:frontend.src.lib.components.assistant.__tests__.assistant_chat_integration:Module]
