Skip to main content

latest
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
76%
Published
4 months ago (0.1.18)
Package root>src>config.test.ts
import { assertEquals } from "jsr:@std/assert@^1.0.8"; import { assertSpyCall, resolvesNext, stub } from "jsr:/@std/testing@^1.0.5/mock"; import { Config } from "./config.ts"; import type { AocConfig } from "./types.ts"; Deno.test("Writing config", async () => { const baseConfig = { year: "2024", days: {}, } satisfies AocConfig; const config = new Config(baseConfig); using writeSpy = stub(Deno, "writeTextFile"); const dayConfig = { 1: { part1: { solved: true, result: 123, tries: 1, runtime: 112, }, part2: { solved: false, result: undefined, tries: 0, }, }, }; await config.write({ days: dayConfig }); assertSpyCall(writeSpy, 0, { args: [ `${Deno.cwd()}/.aoc.json`, JSON.stringify( { year: "2024", days: dayConfig, }, null, 2, ), ], }); }); Deno.test("Writing new day config", async () => { const baseConfig = { year: "2024", days: { 1: { part1: { solved: true, result: 123, tries: 1, runtime: 112, }, part2: { solved: false, result: undefined, tries: 0, }, }, }, } satisfies AocConfig; const config = new Config(baseConfig); const readTextFileStub = stub( Deno, "readTextFile", resolvesNext([JSON.stringify(baseConfig)]), ); using writeSpy = stub(Deno, "writeTextFile"); const dayConfig = { part1: { solved: true, result: 123, tries: 1, runtime: 112, }, part2: { solved: false, tries: 0, }, }; await config.writeDay(2, dayConfig); assertEquals(JSON.parse(writeSpy.calls[0].args[1] as string), { year: "2024", days: { 1: { part1: { solved: true, result: 123, tries: 1, runtime: 112, }, part2: { solved: false, tries: 0, }, }, 2: dayConfig, }, }); readTextFileStub.restore(); }); Deno.test("Overwrites existing day", async () => { const baseConfig = { year: "2024", days: { 1: { part1: { solved: true, result: 123, tries: 1, runtime: 112, }, part2: { solved: false, tries: 0, }, }, }, } satisfies AocConfig; const config = new Config(baseConfig); const readTextFileStub = stub( Deno, "readTextFile", resolvesNext([JSON.stringify(baseConfig)]), ); using writeSpy = stub(Deno, "writeTextFile"); const newConfig = { part2: { solved: true, result: 3, tries: 1, runtime: 123, }, }; await config.writeDay(1, newConfig); assertEquals(JSON.parse(writeSpy.calls[0].args[1] as string), { year: "2024", days: { 1: { part1: { solved: true, result: 123, tries: 1, runtime: 112, }, ...newConfig, }, }, }); readTextFileStub.restore(); });