// #region GitManagerUnfinishedMergeIntegrationTest:Module [TYPE Function]
// @SEMANTICS: git-manager, unfinished-merge, dialog, integration-test
// @PURPOSE: Protect unresolved-merge dialog contract in GitManager pull flow.
// @RELATION DEPENDS_ON -> [GitManager]

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

const MANAGER_PATH = path.resolve(process.cwd(), 'src/components/git/GitManager.svelte');
const UTILS_PATH = path.resolve(process.cwd(), 'src/services/git-utils.ts');
const HANDLER_PATH = path.resolve(process.cwd(), 'src/components/git/useGitManager.ts');
const DIALOG_PATH = path.resolve(process.cwd(), 'src/components/git/GitMergeDialog.svelte');

describe('GitManager unfinished merge dialog contract', () => {
  it('keeps 409 unfinished-merge detection and WebUI dialog recovery flow in pull handler', () => {
    const utils = fs.readFileSync(UTILS_PATH, 'utf-8');
    const handlers = fs.readFileSync(HANDLER_PATH, 'utf-8');
    const manager = fs.readFileSync(MANAGER_PATH, 'utf-8');

    expect(utils).toContain("error_code !== 'GIT_UNFINISHED_MERGE'");
    expect(handlers).toContain('showUnfinishedMergeDialog: true');
    expect(handlers).toContain('openUnfinishedMergeDialogFromError');
    expect(handlers).toContain('await handlers.loadMergeRecoveryState();');
    expect(manager).toContain('model.handlePull');
    expect(manager).toContain('model.loadMergeRecoveryState');
  });

  it('renders unresolved-merge dialog details and web recovery actions', () => {
    const source = fs.readFileSync(DIALOG_PATH, 'utf-8');

    expect(source).toContain('{#if show && unfinishedMergeContext}');
    expect(source).toContain('unfinishedMergeContext.repositoryPath');
    expect(source).toContain('unfinishedMergeContext.currentBranch');
    expect(source).toContain('unfinishedMergeContext.commands.join');
    expect(source).toContain('onCopyCommands');
    expect(source).toContain('onOpenConflictResolver');
    expect(source).toContain('onAbortMerge');
    expect(source).toContain('onContinueMerge');
    expect(source).toContain('$t.git?.unfinished_merge?.copy_commands');
  });
});
// #endregion GitManagerUnfinishedMergeIntegrationTest:Module
