import test from 'node:test';
import assert from 'node:assert/strict';
import {
  buildPlanBranchName,
  filterRetryWorklogEvidencePaths,
  listPlanQuerySchema,
  maskPlanNote,
  normalizeChangedFiles,
  shouldResumePlanDevelopmentFromIssueAction,
} from './plan-service.js';

test('maskPlanNote masks note like the requested preview format', () => {
  assert.equal(maskPlanNote('테스트 작업 실행'), '테** 작업 *행');
});

test('maskPlanNote collapses spaces and masks single-word notes', () => {
  assert.equal(maskPlanNote('  긴급메모  '), '긴***');
  assert.equal(maskPlanNote('가'), '*');
});

test('normalizeChangedFiles removes blanks and duplicate paths while preserving order', () => {
  assert.deepEqual(normalizeChangedFiles([' a.ts ', '', 'a.ts', 'b.ts', '  ', 'b.ts']), ['a.ts', 'b.ts']);
});

test('filterRetryWorklogEvidencePaths keeps non-worklog files and removes repeated worklog artifacts', () => {
  const changedFiles = [
    'docs/worklogs/2026-04-07.md',
    'docs/assets/worklogs/2026-04-07/feature-docs-worklogs.png',
    'src/features/planBoard/PlanBoardPage.tsx',
    'docs/assets/worklogs/2026-04-07/run.log',
  ];
  const existingChangedFilesList = [
    ['docs/worklogs/2026-04-07.md', 'docs/assets/worklogs/2026-04-07/feature-docs-worklogs.png'],
  ];

  assert.deepEqual(filterRetryWorklogEvidencePaths(changedFiles, existingChangedFilesList), [
    'src/features/planBoard/PlanBoardPage.tsx',
    'docs/assets/worklogs/2026-04-07/run.log',
  ]);
});

test('shouldResumePlanDevelopmentFromIssueAction only resumes release-complete items when retry is requested', () => {
  assert.equal(shouldResumePlanDevelopmentFromIssueAction('릴리즈완료', true), true);
  assert.equal(shouldResumePlanDevelopmentFromIssueAction('릴리즈완료', false), false);
  assert.equal(shouldResumePlanDevelopmentFromIssueAction('작업중', true), false);
});

test('buildPlanBranchName uses hotfix prefix for auto-worklog plans and feature prefix otherwise', () => {
  assert.equal(buildPlanBranchName('auto-worklog-2026-04-09', 187), 'hotfix/plan-187-auto-worklog-2026-04-09');
  assert.equal(buildPlanBranchName('new-play-ground', 182), 'feature/plan-182-new-play-ground');
});

test('listPlanQuerySchema accepts legacy filter aliases without throwing', () => {
  assert.deepEqual(listPlanQuerySchema.parse({ status: 'in-progress' }), {
    status: undefined,
  });
  assert.deepEqual(listPlanQuerySchema.parse({ status: 'done' }), {
    status: undefined,
  });
  assert.deepEqual(listPlanQuerySchema.parse({ status: 'error' }), {
    status: undefined,
  });
  assert.deepEqual(listPlanQuerySchema.parse({ status: 'all' }), {
    status: undefined,
  });
});

test('listPlanQuerySchema keeps exact plan statuses and rejects unknown values', () => {
  assert.deepEqual(listPlanQuerySchema.parse({ status: '작업중' }), {
    status: '작업중',
  });
  assert.throws(() => listPlanQuerySchema.parse({ status: 'processing' }));
});
