Skip to content
TypeScript
2.9 KB
Raw Blame
/**
 * 로컬 벤치 (PRD §11). 파일로 검증 가능한 단순 과제(스모크/해피패스).
 * verify/setup 는 오프라인·파일만, 절대 throw 금지(fail-soft).
 */
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";

export type Difficulty = "easy" | "medium" | "hard";

export interface LocalTask {
  id: string;
  instruction: string;
  difficulty?: Difficulty;
  /** 입력 픽스처를 work_dir 에 결정적으로 생성(순수·RNG/시계 금지). */
  setup?: (workDir: string) => void;
  /** 숨은 검증 — true=정답. 절대 throw 금지. */
  verify: (workDir: string) => boolean;
}

function readSafe(workDir: string, name: string): string | null {
  try {
    return readFileSync(join(workDir, name), "utf8");
  } catch {
    return null;
  }
}

function guard(fn: () => boolean): boolean {
  try {
    return fn();
  } catch {
    return false;
  }
}

export const LOCAL_TASKS: LocalTask[] = [
  {
    id: "local-hello",
    difficulty: "easy",
    instruction:
      "Create a file named hello.txt in the working directory whose exact contents are: hello world",
    verify: (work) => guard(() => readSafe(work, "hello.txt")?.trim() === "hello world"),
  },
  {
    id: "local-sum",
    difficulty: "easy",
    instruction:
      "Read numbers.txt (one integer per line). Write their sum as a single integer to sum.txt.",
    setup: (work) => writeFileSync(join(work, "numbers.txt"), "3\n8\n15\n21\n53\n"),
    verify: (work) =>
      guard(() => {
        const out = readSafe(work, "sum.txt");
        return out !== null && Number.parseInt(out.trim(), 10) === 100;
      }),
  },
  {
    id: "local-reverse",
    difficulty: "easy",
    instruction:
      "Read input.txt and write its single line reversed (characters reversed) to output.txt.",
    setup: (work) => writeFileSync(join(work, "input.txt"), "deterministic"),
    verify: (work) =>
      guard(
        () => readSafe(work, "output.txt")?.trim() === "deterministic".split("").reverse().join(""),
      ),
  },
  {
    id: "local-config",
    difficulty: "medium",
    instruction:
      'Create config.json: a JSON object with key "version" set to the integer 2 and key "name" set to the string "theseus".',
    verify: (work) =>
      guard(() => {
        const raw = readSafe(work, "config.json");
        if (!raw) return false;
        const obj = JSON.parse(raw);
        return obj.version === 2 && obj.name === "theseus";
      }),
  },
  {
    id: "local-wordcount",
    difficulty: "medium",
    instruction:
      "Read text.txt and write the number of whitespace-separated words as an integer to count.txt.",
    setup: (work) =>
      writeFileSync(join(work, "text.txt"), "the quick brown fox jumps over the lazy dog"),
    verify: (work) =>
      guard(() => {
        const out = readSafe(work, "count.txt");
        return out !== null && Number.parseInt(out.trim(), 10) === 9;
      }),
  },
];