justintsus-experiment Public
// Canonical serialization and round trips (BRIEF.md, "Canonical form").import assert from "node:assert/strict";import { describe, test } from "node:test";import { api, canon, CTX, parsed } from "./helpers.mjs";const PAIRS = [ ["IS:Open", "is:open"], ['label:" Needs Review "', 'label:"needs review"'], ['label:"bug"', "label:bug"], ["author:@ME", "author:@me"], ['author:"Fixer"', "author:fixer"], ["author:AAAAAAAA-1111-4111-8111-111111111111", "author:aaaaaaaa-1111-4111-8111-111111111111"], ["updated:<007D", "updated:<7d"], ["updated:>=2W created:<=12H", "updated:>=2w created:<=12h"], ["created:2026-10-01..*", "created:2026-10-01..*"], ["created:*..2026-10-01 created:>=2026-10-01", "created:*..2026-10-01 created:>=2026-10-01"], ['"bug"', "bug"], ['"needs review"', '"needs review"'], ['"two spaces"', '"two spaces"'], ['"#12" #12', '"#12" #12'], ['"-x" --x', '"-x" -"-x"'], ['-label:Bug -"Foo Bar" -Crash', '-label:bug -"Foo Bar" -Crash'], ["Crash crash", "Crash crash"], ["label:bug label:BUG is:open is:open #1 #1", "label:bug is:open #1"], ["sort:updated-asc is:open label:bug", "sort:updated-asc is:open label:bug"], [" is:open \t\n label:bug ", "is:open label:bug"], ['a\\b "c\\d e"', 'a\\b "c\\\\d e"'], ["no:LABEL", "no:label"],];describe("serialize: canonical form", () => { test("normalization, quoting and duplicates follow the canonical rules", () => { for (const [input, want] of PAIRS) assert.equal(canon(input), want, `${JSON.stringify(input)} is written ${JSON.stringify(want)}`); }); test("pull request qualifiers serialize the same way", () => { assert.equal(canon("IS:Merged -IS:DRAFT Review-Requested:@ME", "pulls"), "is:merged -is:draft review-requested:@me"); }); test("the canonical form of the empty query is the empty string", () => { assert.equal(api.serializeQuery(parsed("")), ""); });});const CORPUS = [ "", "is:open", "is:closed label:bug -label:wontfix", 'label:"needs review" no:label', "author:@me author:fixer", "sort:comments-desc", "#123", 'crash "quoted phrase" -"not this"', "created:>2026-10-01 updated:<7d", "created:2026-09-30..2026-10-01", "Ünïcode 🐛 日本語", '"say \\"hi\\""', 'a\\b "x\\\\y z"', "100% a_c", "-", "--x", '"label:x" "#5" "-y"', "12:30 a1:b :x", "IS:OPEN LABEL:BUG", "is:open label:bug", "sort:created-asc sort:created-asc", "updated:>=0001w",];describe("serialize: round trips", () => { test("serialize is idempotent: the canonical form parses back to itself", () => { for (const x of CORPUS) { const c = canon(x); assert.equal(canon(c), c, `for ${JSON.stringify(x)}`); } }); test("the canonical form evaluates to the same SQL and params as the input", () => { for (const x of CORPUS) { const a = api.toSql(parsed(x), CTX); const b = api.toSql(parsed(canon(x)), CTX); assert.deepEqual(b, a, `for ${JSON.stringify(x)}`); } }); test("pulls: idempotent and the same SQL", () => { for (const x of ["is:merged -is:draft", "review-requested:@me author:fixer", "sort:updated-asc #4 refactor", "IS:Draft", '"a b" -c']) { const c = canon(x, "pulls"); assert.equal(canon(c, "pulls"), c); assert.deepEqual(api.toSql(parsed(c, "pulls"), CTX), api.toSql(parsed(x, "pulls"), CTX)); } }); test("the canonical form survives a URL's query string", () => { for (const x of CORPUS) { const c = canon(x); const url = new URL(`https://rebasely.test/acme/app/issues?${new URLSearchParams({ q: c })}`); const back = url.searchParams.get("q"); assert.equal(back, c); assert.equal(canon(back), c); } }); test("terms keep their order (the canonical form is not sorted)", () => { assert.equal(canon("label:bug is:open"), "label:bug is:open"); assert.equal(canon("is:open label:bug"), "is:open label:bug"); }); test("differently written equal terms collapse into the first", () => { assert.equal(canon('label:BUG label:"bug" label:" bug "'), "label:bug"); assert.equal(canon("updated:<7d updated:<07D"), "updated:<7d"); });});