const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');

const root = path.join(__dirname, '..');

function read(relativePath) {
  return fs.readFileSync(path.join(root, relativePath), 'utf8');
}

test('clinical assistant starter prompts are Redis or indexed-source backed', () => {
  const route = read('src/routes/clinicalAssistant.js');
  const pool = read('src/utils/clinicalPromptPool.js');
  assert.doesNotMatch(route, /EXAMPLE_CANDIDATES|TOPIC_SUGGESTIONS|topic_suggestions|buildSuggestionAnswer/);
  assert.match(route, /createClinicalPromptPool\(\{[\s\S]*redisCache: redisCache/);
  assert.match(route, /isUsefulIndexedTopicExample/);
  assert.match(pool, /clinical-assistant:prompt-pool:v2/);
  assert.match(pool, /redisBaseKey \+ ':all'/);
  assert.match(pool, /redisBaseKey \+ ':last-good'/);
  assert.match(pool, /redisBaseKey \+ ':category:' \+ category/);
  assert.match(pool, /redisBaseKey \+ ':age:' \+ ageBand/);
  assert.match(pool, /redisBaseKey \+ ':intent:' \+ intent/);
  assert.match(pool, /CLINICAL_ASSISTANT_PROMPT_POOL_TARGET, 1000/);
  assert.match(pool, /CLINICAL_ASSISTANT_PROMPT_POOL_REFRESH_MS, 7 \* 24 \* 60 \* 60 \* 1000/);
  assert.match(route, /loadStoredPromptPool: loadLatestPromptPoolSnapshot/);
  assert.match(pool, /PEDIATRIC_TAXONOMY/);
  assert.match(pool, /taxonomyWithQuotas\(target\)/);
  assert.match(pool, /age_band: ageBand/);
  assert.match(pool, /function hasPediatricSignal\(prompt, item\)/);
  assert.match(pool, /var examples = await fallbackIndexedExamples\(\)/);
  assert.doesNotMatch(pool, /taxonomyFallbackExamples|taxonomySupplementExamples/);
  assert.doesNotMatch(pool, /return fallbackIndexedExamples\(\)/);
});

test('clinical assistant prompt pool can be regenerated by admins', () => {
  const admin = read('src/routes/adminConfig.js');
  const page = read('public/components/admin.html');
  const js = read('public/js/admin.js');
  assert.match(admin, /\/clinical-assistant\/prompt-pool\/regenerate/);
  assert.match(admin, /\/clinical-assistant\/prompt-pool\/restore/);
  assert.match(page, /btn-regenerate-assistant-prompt-pool/);
  assert.match(page, /btn-restore-assistant-prompt-pool/);
  assert.match(js, /regenerateAssistantPromptPool/);
  assert.match(js, /restoreAssistantPromptPool/);
});

test('clinical assistant prompt forbids relabeling other sources as named sources', () => {
  const answer = read('src/utils/clinicalAnswer.js');
  assert.match(answer, /If the user names a specific source, textbook, guideline, or table/);
  assert.match(answer, /do not claim that another source is from the named source/);
  assert.match(answer, /If the named source is absent from the retrieved sources, say that explicitly/);
});

test('clinical assistant prompt requires bracketed citations in table source columns', () => {
  const answer = read('src/utils/clinicalAnswer.js');
  assert.match(answer, /Source, Source\(s\), Citation, or Citation\(s\) column/);
  assert.match(answer, /must use bracketed citation tokens like \[1\] or \[1, 3\]/);
  assert.match(answer, /never bare numbers like 1 or 1, 3/);
});
