import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import { tmpdir } from 'os';
import { executeSetTheme } from './set-theme.js';
import { readSlidesDoc } from './state.js';

function makeCtx(ws: string) {
  return { workspacePath: ws } as any;
}

describe('SetTheme', () => {
  let ws: string;
  beforeEach(() => { ws = fs.mkdtempSync(path.join(tmpdir(), 'slide-st-')); });
  afterEach(() => { fs.rmSync(ws, { recursive: true, force: true }); });

  it('sets a valid preset', async () => {
    const r = await executeSetTheme({ preset: 'corporate-blue' }, makeCtx(ws));
    expect(r.isError).toBeFalsy();
    expect(readSlidesDoc(ws).theme.preset).toBe('corporate-blue');
  });

  it('applies overrides', async () => {
    const r = await executeSetTheme(
      { preset: 'minimal-mono', overrides: { primary: '#FF0000' } },
      makeCtx(ws),
    );
    expect(r.isError).toBeFalsy();
    expect(readSlidesDoc(ws).theme.overrides.primary).toBe('#FF0000');
  });

  it('rejects unknown preset', async () => {
    const r = await executeSetTheme({ preset: 'invalid' }, makeCtx(ws));
    expect(r.isError).toBe(true);
    expect(r.output).toMatch(/preset/i);
  });

  it('rejects missing preset', async () => {
    const r = await executeSetTheme({}, makeCtx(ws));
    expect(r.isError).toBe(true);
  });
});
