Download src/lib/coding/validate.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 3.48 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/coding/validate.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/coding/validate.ts
-
curl -L -o validate.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/coding/validate.ts
3.48 kB
| import { runJavaScriptTests } from "./harness"; | |
| import { isExecutableLanguage } from "./types"; | |
| import type { CodingProblem } from "./types"; | |
| // --------------------------------------------------------------------------- | |
| // Problem validation — guarantees every generated problem has a known, | |
| // verifiable answer before it reaches the user. | |
| // | |
| // - executable (code-creation / debugging): the reference solution must pass | |
| // all tests; debugging additionally requires the buggy code to fail at | |
| // least one test (otherwise there is nothing to fix). | |
| // - code-review: issue ids must be unique and the code non-empty. | |
| // - architecture: the rubric must be non-empty with unique ids and positive | |
| // weights. | |
| // | |
| // For non-executable languages (python, go) we can't run the reference, so we | |
| // fall back to structural checks and trust the generator's self-consistency. | |
| // --------------------------------------------------------------------------- | |
| export interface ValidationResult { | |
| valid: boolean; | |
| reason?: string; | |
| } | |
| const ok: ValidationResult = { valid: true }; | |
| function fail(reason: string): ValidationResult { | |
| return { valid: false, reason }; | |
| } | |
| export function validateProblem(problem: CodingProblem): ValidationResult { | |
| switch (problem.type) { | |
| case "code-creation": | |
| return validateExecutable( | |
| problem.language, | |
| problem.entryFunction, | |
| problem.referenceSolution, | |
| problem.tests, | |
| ); | |
| case "debugging": { | |
| const refResult = validateExecutable( | |
| problem.language, | |
| problem.entryFunction, | |
| problem.referenceSolution, | |
| problem.tests, | |
| ); | |
| if (!refResult.valid) return refResult; | |
| // The buggy code must actually be broken against the tests. | |
| if (isExecutableLanguage(problem.language)) { | |
| const buggyRun = runJavaScriptTests( | |
| problem.buggyCode, | |
| problem.entryFunction, | |
| problem.tests, | |
| ); | |
| if (buggyRun.allPassed) { | |
| return fail("buggy code passes every test — nothing to fix"); | |
| } | |
| } | |
| return ok; | |
| } | |
| case "code-review": { | |
| if (problem.code.trim().length === 0) return fail("empty code under review"); | |
| const ids = problem.issues.map((i) => i.id); | |
| if (new Set(ids).size !== ids.length) return fail("duplicate issue ids"); | |
| return ok; | |
| } | |
| case "architecture": { | |
| if (problem.rubric.length === 0) return fail("empty rubric"); | |
| const ids = problem.rubric.map((r) => r.id); | |
| if (new Set(ids).size !== ids.length) return fail("duplicate rubric ids"); | |
| if (problem.rubric.some((r) => r.weight <= 0)) { | |
| return fail("rubric weights must be positive"); | |
| } | |
| return ok; | |
| } | |
| } | |
| } | |
| function validateExecutable( | |
| language: string, | |
| entryFunction: string, | |
| referenceSolution: string, | |
| tests: { name: string; input: unknown[]; expected: unknown }[], | |
| ): ValidationResult { | |
| if (!tests || tests.length === 0) return fail("no tests provided"); | |
| // We can only execute JS-family languages; trust structural shape otherwise. | |
| if (!isExecutableLanguage(language)) return ok; | |
| const run = runJavaScriptTests(referenceSolution, entryFunction, tests); | |
| if (run.compileError) { | |
| return fail(`reference solution did not compile: ${run.compileError}`); | |
| } | |
| if (!run.allPassed) { | |
| return fail( | |
| `reference solution failed ${run.total - run.passed}/${run.total} tests`, | |
| ); | |
| } | |
| return ok; | |
| } | |