Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion test/unit/json-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { describe, expect, it } from "vitest";
import { errorMessage, jsonString, normalizeRepoFullName, parseJson, repoParts, strippedErrorMessage } from "../../src/utils/json";
import { errorMessage, jsonString, normalizeRepoFullName, nowIso, parseJson, repoParts, strippedErrorMessage } from "../../src/utils/json";

describe("JSON and string utility helpers", () => {
it("keeps JSON parsing and stringification fallbacks explicit", () => {
expect(parseJson<{ ok: boolean }>('{"ok":true}', { ok: false })).toEqual({ ok: true });
expect(parseJson("{bad-json", { ok: false })).toEqual({ ok: false });
expect(parseJson(null, { ok: false })).toEqual({ ok: false });
expect(parseJson("", { ok: false })).toEqual({ ok: false });
expect(parseJson(undefined, { ok: false })).toEqual({ ok: false });
expect(jsonString(undefined)).toBe("null");
expect(jsonString({ ok: true })).toBe('{"ok":true}');
});

it("emits ISO timestamps and empty error fallbacks", () => {
expect(nowIso()).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
expect(errorMessage(new Error(""))).toBe("unknown error");
});

it("normalizes repo names and error messages without leaking arbitrary thrown values", () => {
expect(normalizeRepoFullName(" JSONbored/gittensory ")).toBe("JSONbored/gittensory");
expect(repoParts("JSONbored/gittensory")).toEqual({ owner: "JSONbored", name: "gittensory" });
Expand Down