import test from 'node:test';
import assert from 'node:assert/strict';
import { buildPlanNotificationData } from './plan-notification-service.js';
import { shouldNotifyPlanRestart } from './plan-notification-policy.js';
import { shouldTriggerRetryFromActionNote } from './plan-retry-policy.js';
import { issueActionSchema, shouldUseLocalMainPlanMode } from './plan-service.js';

test('shouldTriggerRetryFromActionNote detects missing-fix and verification follow-up requests', () => {
  assert.equal(shouldTriggerRetryFromActionNote('누락된 거 다시 고쳐서 테스트해 줘'), true);
  assert.equal(shouldTriggerRetryFromActionNote('빠진 내용 보완하고 검증해줘'), true);
});

test('shouldTriggerRetryFromActionNote ignores simple status comments', () => {
  assert.equal(shouldTriggerRetryFromActionNote('확인했습니다. 메모만 남깁니다.'), false);
});

test('issueActionSchema defaults retry to false', () => {
  assert.deepEqual(
    issueActionSchema.parse({
      actionNote: '원인 분석과 조치 내용을 남깁니다.',
    }),
    {
      actionNote: '원인 분석과 조치 내용을 남깁니다.',
      resolve: false,
      retry: false,
    },
  );
});

test('shouldNotifyPlanRestart only allows restart alerts when a retry was actually scheduled', () => {
  assert.equal(
    shouldNotifyPlanRestart({
      didScheduleRetry: false,
    }),
    false,
  );
  assert.equal(
    shouldNotifyPlanRestart({
      didScheduleRetry: true,
    }),
    true,
  );
  assert.equal(shouldNotifyPlanRestart(null), false);
});

test('buildPlanNotificationData uses stable task key per plan', () => {
  assert.deepEqual(buildPlanNotificationData(17, 'WK-123', 'plan-restarted'), {
    planId: '17',
    workId: 'WK-123',
    eventType: 'plan-restarted',
    targetUrl: 'https://sm-home.cloud/?topMenu=plans&planId=17&workId=WK-123',
    notificationKey: 'plan:17',
  });
});

test('shouldUseLocalMainPlanMode keeps auto_worker on branch workflow', () => {
  process.env.PLAN_LOCAL_MAIN_MODE = 'true';
  assert.equal(shouldUseLocalMainPlanMode('auto_worker'), false);
  assert.equal(shouldUseLocalMainPlanMode('none'), true);
});
