import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {
  buildInitialUserMemoryProfile,
  buildSessionMemoryEntries,
  ensureUserMemoryProfile,
  hasMemoryStore,
  loadUserMemoryProfile,
  renderMemoryStoreGuidance,
  USER_MEMORY_PROFILE_FILENAME,
} from './user-memory-profile.mjs';
import { buildAgentExtensionPolicy, DEFAULT_USER_CAPABILITIES } from './capabilities.mjs';

test('DEFAULT_USER_CAPABILITIES enables memory_store (L3)', () => {
  assert.equal(DEFAULT_USER_CAPABILITIES.memory_store, true);
  assert.equal(DEFAULT_USER_CAPABILITIES.context_memory, true);
});

test('buildAgentExtensionPolicy adds builtin memory when memory_store is enabled', () => {
  const policy = buildAgentExtensionPolicy(DEFAULT_USER_CAPABILITIES);
  assert.ok(policy.extensionOverrides.some((ext) => ext.name === 'memory'));
  assert.ok(policy.extensionOverrides.some((ext) => ext.name === 'projectmemory'));
});

test('hasMemoryStore detects memory extension in session policy', () => {
  const policy = buildAgentExtensionPolicy({ ...DEFAULT_USER_CAPABILITIES, memory_store: false });
  assert.equal(hasMemoryStore(policy), false);
  assert.equal(hasMemoryStore(buildAgentExtensionPolicy(DEFAULT_USER_CAPABILITIES)), true);
});

test('ensureUserMemoryProfile creates and updates profile file', () => {
  const root = fs.mkdtempSync(path.join(os.tmpdir(), 'tkmind-profile-'));
  const first = ensureUserMemoryProfile(root, {
    userId: 'user-1',
    displayName: 'Alice',
    username: 'alice',
  });
  assert.equal(first.displayName, 'Alice');
  assert.equal(fs.existsSync(path.join(root, USER_MEMORY_PROFILE_FILENAME)), true);

  const updated = ensureUserMemoryProfile(root, {
    userId: 'user-1',
    displayName: '艾丽丝',
    username: 'alice',
  });
  assert.equal(updated.displayName, '艾丽丝');
  assert.equal(loadUserMemoryProfile(root)?.displayName, '艾丽丝');
});

test('buildSessionMemoryEntries injects sandbox, guidance, and profile', () => {
  const root = fs.mkdtempSync(path.join(os.tmpdir(), 'tkmind-memory-'));
  ensureUserMemoryProfile(root, {
    userId: 'user-2',
    displayName: 'Bob',
    username: 'bob',
  });
  const sessionPolicy = buildAgentExtensionPolicy(DEFAULT_USER_CAPABILITIES);
  const entries = buildSessionMemoryEntries({
    workingDir: root,
    sessionPolicy,
    sandboxConstraints: '## sandbox',
    userContext: { userId: 'user-2', displayName: 'Bob', username: 'bob' },
  });
  assert.equal(entries.length, 3);
  assert.equal(entries[0].title, 'TKMind 用户空间沙箱');
  assert.match(entries[1].content, /长期记忆/);
  assert.match(entries[2].content, /Bob/);
});

test('renderMemoryStoreGuidance scopes memory to one user', () => {
  const text = renderMemoryStoreGuidance({ addressName: '小陈' });
  assert.match(text, /小陈/);
  assert.match(text, /不得与其他用户混淆/);
});

test('buildInitialUserMemoryProfile seeds defaults', () => {
  const profile = buildInitialUserMemoryProfile({
    userId: 'abc',
    displayName: '测试',
    username: 'tester',
  });
  assert.equal(profile.version, 1);
  assert.equal(profile.language, 'zh-CN');
  assert.equal(profile.responseStyle, 'balanced');
  assert.deepEqual(profile.preferences, []);
});
