import { afterEach, describe, expect, test, vi } from 'vitest';

const load = async () => {
  vi.resetModules();
  process.env.SPOON_WORKER_TOKEN = 'test-worker-token';
  process.env.GITHUB_APP_ID = '123';
  process.env.GITHUB_APP_PRIVATE_KEY =
    '-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----';
  process.env.SPOON_AGENT_WORKDIR = '/work';
  return await import('../../src/server');
};

describe('boxRoute', () => {
  afterEach(() => vi.resetModules());

  test('matches the four box actions', async () => {
    const { boxRoute } = await load();
    expect(boxRoute('/box/status')).toEqual({ action: 'status' });
    expect(boxRoute('/box/lifecycle')).toEqual({ action: 'lifecycle' });
    expect(boxRoute('/box/tree')).toEqual({ action: 'tree' });
    expect(boxRoute('/box/file')).toEqual({ action: 'file' });
  });

  test('rejects unknown, nested, and trailing paths', async () => {
    const { boxRoute } = await load();
    expect(boxRoute('/box/other')).toBeNull();
    expect(boxRoute('/box/file/extra')).toBeNull();
    expect(boxRoute('/box/status/')).toBeNull();
    expect(boxRoute('/box')).toBeNull();
    expect(boxRoute('/jobs/x/file')).toBeNull();
  });
});
