import { describe, it, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import { join } from 'path';
import { runHook, setupTestDir, cleanupTestDir, readState, readJsonl } from './test-helper.mjs';

let dir;
afterEach(() => { if (dir) cleanupTestDir(dir); });

describe('session-start', () => {
  it('creates state file and emits context', () => {
    dir = setupTestDir();
    const out = runHook('session-start.mjs', { session_id: 's1', cwd: '/tmp' }, dir);
    assert.equal(out.continue, true);
    assert.ok(out.hookSpecificOutput.additionalContext.includes('Interaction Awareness is active'));
    const state = readState(dir, 's1');
    assert.ok(state);
    assert.equal(state.tool_count, 0);
    assert.equal(state.edit_count, 0);
    assert.equal(state.dep_flags, 0);
  });

  it('writes start record to sessions.jsonl', () => {
    dir = setupTestDir();
    runHook('session-start.mjs', { session_id: 's2', cwd: '/tmp' }, dir);
    const records = readJsonl(join(dir, 'sessions.jsonl'));
    assert.equal(records.length, 1);
    assert.equal(records[0].session_id, 's2');
    assert.ok('hour' in records[0]);
    assert.ok('is_late_night' in records[0]);
  });

  it('state has correct initial fields', () => {
    dir = setupTestDir();
    runHook('session-start.mjs', { session_id: 's3', cwd: '/tmp' }, dir);
    const state = readState(dir, 's3');
    assert.equal(state.burst_count, 0);
    assert.equal(state.last_event_epoch, 0);
    assert.equal(state.last_warning_epoch, 0);
    assert.ok(state.start_epoch > 0);
    assert.ok(state.start_iso.length > 0);
  });

  it('returns continue with no side effects when session_id missing', () => {
    dir = setupTestDir();
    const out = runHook('session-start.mjs', { cwd: '/tmp' }, dir);
    assert.equal(out.continue, true);
    assert.ok(!out.hookSpecificOutput);
  });
});
