UNPKG

@firfi/quint-connect

Version:

Model-based testing framework connecting Quint specifications to TypeScript implementations

52 lines 2.61 kB
import { Effect, Schema } from "effect"; import { readdir, readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { ItfTrace } from "../itf/schema.js"; import { QuintError } from "./errors.js"; const traceFileName = (index) => `trace_${index}.itf.json`; const stringifyItfJson = (trace) => JSON.stringify(trace, (_key, value) => typeof value === "bigint" ? { "#bigint": String(value) } : value); const serializeItfTrace = (trace) => Effect.gen(function* () { const content = yield* Effect.try({ try: () => stringifyItfJson(trace), catch: (e) => new QuintError({ message: `Failed to serialize ITF trace: ${e}` }) }); const json = yield* Effect.try({ try: () => JSON.parse(content), catch: (e) => new QuintError({ message: `Failed to serialize valid ITF JSON: ${e}` }) }); yield* Schema.decodeUnknown(ItfTrace)(json).pipe(Effect.mapError((e) => new QuintError({ message: `Failed to validate ITF trace for writing: ${e}` }))); return content; }); export const writeTraceFiles = (outDir, traces) => Effect.gen(function* () { const contents = yield* Effect.forEach(traces, serializeItfTrace); return yield* Effect.tryPromise({ try: () => Promise.all(contents.map(async (content, index) => { const filePath = join(outDir, traceFileName(index)); await writeFile(filePath, content); return filePath; })), catch: (e) => new QuintError({ message: `Failed to write trace file: ${e}` }) }); }); export const readTraceFiles = (outDir) => Effect.gen(function* () { const files = yield* Effect.tryPromise({ try: () => readdir(outDir), catch: (e) => new QuintError({ message: `Failed to read trace directory: ${e}` }) }); const traceFiles = files.filter((file) => file.endsWith(".itf.json")).sort(); const traces = []; for (const file of traceFiles) { const content = yield* Effect.tryPromise({ try: () => readFile(join(outDir, file), "utf-8"), catch: (e) => new QuintError({ message: `Failed to read trace file ${file}: ${e}` }) }); const json = yield* Effect.try({ try: () => JSON.parse(content), catch: (e) => new QuintError({ message: `Invalid JSON in trace file ${file}: ${e}` }) }); const trace = yield* Effect.mapError(Schema.decodeUnknown(ItfTrace)(json), (e) => new QuintError({ message: `Failed to parse ITF trace ${file}: ${e}` })); traces.push(trace); } return traces; }); //# sourceMappingURL=trace-files.js.map