// Tests for src/context.js — system prompt builder
const fs = require('fs');

jest.mock('../src/constants');
jest.mock('../src/ollama', () => ({
  streamChat: jest.fn().mockResolvedValue({ response: '[]' }),
}));

const { CONNECTIONS_FILE } = require('../src/constants');
const context = require('../src/context');

describe('context', () => {
  beforeEach(() => {
    context.invalidateConnectionsCache();
  });

  describe('getActiveConnections', () => {
    test('returns boolean fields for each service', () => {
      const c = context.getActiveConnections();
      expect(c).toHaveProperty('brave');
      expect(c).toHaveProperty('github');
      expect(c).toHaveProperty('google');
      expect(c).toHaveProperty('telegram');
      expect(c).toHaveProperty('jira');
      expect(c).toHaveProperty('dynamics365');
    });

    test('all false with empty connections', () => {
      fs.writeFileSync(CONNECTIONS_FILE, '{}');
      const c = context.getActiveConnections();
      for (const val of Object.values(c)) {
        expect(val).toBe(false);
      }
    });

    test('detects active github key', () => {
      fs.writeFileSync(CONNECTIONS_FILE, JSON.stringify({ github_key: 'ghp_x' }));
      expect(context.getActiveConnections().github).toBe(true);
      fs.writeFileSync(CONNECTIONS_FILE, '{}');
    });
  });

  describe('buildSystemPrompt', () => {
    test('returns a non-empty string', () => {
      const prompt = context.buildSystemPrompt('/tmp');
      expect(typeof prompt).toBe('string');
      expect(prompt.length).toBeGreaterThan(100);
    });

    test('includes current date', () => {
      const prompt = context.buildSystemPrompt('/tmp');
      const year = new Date().getFullYear().toString();
      expect(prompt).toContain(year);
    });
  });

  describe('isComfyUIInstalled', () => {
    test('returns false when comfyui.json missing', () => {
      expect(context.isComfyUIInstalled()).toBe(false);
    });
  });

  describe('self-awareness context', () => {
    test('buildSystemPrompt includes self_awareness block when data exists', () => {
      // The mock environment has cron.json, sessions, memory etc.
      // Even with minimal data, the function should not throw.
      const prompt = context.buildSystemPrompt('/tmp');
      expect(typeof prompt).toBe('string');
      // The prompt should always contain the date and working directory
      expect(prompt).toContain('Working directory: /tmp');
    });

    test('self-awareness does not crash with empty modules', () => {
      // All self-awareness calls are try/catch wrapped, so even
      // with broken or missing modules, the prompt should still build.
      const prompt = context.buildSystemPrompt('/tmp');
      expect(prompt.length).toBeGreaterThan(100);
    });

    test('self-awareness block appears after GPU status', () => {
      const prompt = context.buildSystemPrompt('/tmp');
      const selfIdx = prompt.indexOf('<self_awareness>');
      const ctxIdx = prompt.indexOf('<context>');
      // If self_awareness exists, it should appear before <context>
      if (selfIdx >= 0) {
        expect(selfIdx).toBeLessThan(ctxIdx);
      }
    });
  });
});
