import { describe, expect, it } from 'vitest';
import {
  DEFAULT_LEARNITIVE_MAIN_TEX,
  initializeBlankLearnitiveProject,
} from '../project-bootstrap';
import { MemoryProjectStorage } from '../memory-project-storage';
import { projectFilePath } from '../project-path';

describe('initializeBlankLearnitiveProject', () => {
  it('creates a canonical main.tex and figures directory for an empty project', async () => {
    const storage = new MemoryProjectStorage();

    await expect(initializeBlankLearnitiveProject(storage)).resolves.toEqual({
      created: true,
      rootFile: 'main.tex',
    });

    const entries = await storage.listEntries();
    expect(entries).toEqual(
      expect.arrayContaining([
        expect.objectContaining({ kind: 'directory', path: 'figures/' }),
        expect.objectContaining({ kind: 'file', path: 'main.tex' }),
      ]),
    );
    expect(
      new TextDecoder().decode(await storage.readFile(projectFilePath('main.tex'))),
    ).toBe(DEFAULT_LEARNITIVE_MAIN_TEX);
    expect(entries.some((entry) => entry.path.endsWith('.md'))).toBe(false);
  });

  it('does not overwrite or seed an existing project', async () => {
    const storage = new MemoryProjectStorage();
    await storage.writeFile(
      projectFilePath('existing.tex'),
      new TextEncoder().encode('existing'),
    );

    await expect(initializeBlankLearnitiveProject(storage)).resolves.toEqual({
      created: false,
      rootFile: undefined,
    });
    await expect(storage.readFile(projectFilePath('main.tex'))).resolves.toBeUndefined();
  });
});
