Rebasely Beta: data may be purged

justintsus-experiment Public

JavaScript · 239 lines · 10.6 KiB · at 8e57616

// Parsing: terms, quotes, negation, unknown and unsupported qualifiers, empty input, unicode, limits,// and error codes with their spans (BRIEF.md, "The query language").import assert from "node:assert/strict";import { describe, test } from "node:test";import { api, canon, codes, failed, parsed } from "./helpers.mjs";describe("parse: basics", () => {  test("the empty input and whitespace-only input are valid, empty queries", () => {    assert.equal(canon(""), "");    assert.equal(canon("   \t\n  "), "");    assert.equal(canon("", "pulls"), "");  });  test("every supported qualifier parses for issues", () => {    parsed('is:open is:closed label:bug -label:wontfix label:"needs review" no:label author:@me author:fixer');    parsed("sort:updated-desc #123 crash \"quoted phrase\" created:>2026-10-01 updated:<7d sort:updated-desc");    parsed("sort:comments-asc");  });  test("every supported qualifier parses for pulls", () => {    parsed("is:open is:closed is:merged is:draft -is:draft author:@me review-requested:@me -review-requested:fixer", "pulls");    parsed("sort:created-asc #7 \"a phrase\" created:2026-10-01..2026-10-05 updated:>=2w", "pulls");  });  test("parseQuery never throws on odd strings", () => {    for (const s of ["\u0000", "\\", '"', "-", "--", "#", ":", "::", "-:", "a:b:c", "\ud800", "\udfff", "‮", "🐛".repeat(10), "'".repeat(30)]) {      for (const t of ["issues", "pulls"]) {        const r = api.parseQuery(s, t);        assert.equal(typeof r.ok, "boolean", JSON.stringify(s));      }    }  });});describe("parse: kinds of terms", () => {  test("text that is not a qualifier stays free text: 12:30, a1:b, :x", () => {    assert.equal(canon("12:30 a1:b :x"), "12:30 a1:b :x");  });  test("#n with 1 to 9 digits and no leading zero is a number; anything else is text", () => {    assert.equal(canon("#123"), "#123");    assert.equal(canon("#0123 #1234567890 #abc #"), "#0123 #1234567890 #abc #");  });  test("a lone - is the free-text word -", () => {    assert.equal(canon("-"), '"-"');  });  test("a quoted qualifier is a phrase, not a qualifier", () => {    assert.equal(canon('"label:bug"'), '"label:bug"');    assert.equal(canon('"is:bogus"'), '"is:bogus"');  });  test("OR is a word, not an operator", () => {    assert.equal(canon("is:open OR is:closed"), "is:open OR is:closed");  });  test("qualifier keys are case-insensitive", () => {    assert.equal(canon("IS:Open LABEL:Bug Author:@Me"), "is:open label:bug author:@me");  });  test("negation applies to qualifiers and to free text", () => {    assert.equal(canon('-label:bug -is:open -author:@me -crash -"two words"'), '-label:bug -is:open -author:@me -crash -"two words"');  });});describe("parse: quotes", () => {  test("a qualifier takes a quoted value", () => {    assert.equal(canon('label:"needs review"'), 'label:"needs review"');  });  test('\\" and \\\\ are escapes inside quotes; another backslash stays', () => {    assert.equal(canon('"say \\"hi\\" now"'), '"say \\"hi\\" now"');    assert.equal(canon('"a\\\\b c"'), '"a\\\\b c"');    assert.equal(canon('"a\\b c"'), '"a\\\\b c"');  });  test("an unterminated quote is one error from the term to the end, and parsing stops", () => {    const e = failed('is:open "abc is:bogus');    assert.deepEqual(e.map((x) => [x.code, x.start, x.end]), [["unterminated_quote", 8, 21]]);  });  test("an unterminated qualifier quote spans the whole term", () => {    const s = 'label:"needs review';    assert.deepEqual(failed(s).map((x) => [x.code, x.start, x.end]), [["unterminated_quote", 0, s.length]]);    assert.deepEqual(codes('"abc\\"'), ["unterminated_quote"], "an escaped quote does not close the value");  });  test("text right after a closing quote is unexpected_quote", () => {    assert.deepEqual(codes('"abc"def'), ["unexpected_quote"]);    assert.deepEqual(codes('label:"bug"x'), ["unexpected_quote"]);  });  test("a quote inside bare text is unexpected_quote, spanning to the next whitespace", () => {    const e = failed('ok foo"bar is:open');    assert.deepEqual(e.map((x) => [x.code, x.start, x.end]), [["unexpected_quote", 3, 10]]);  });  test('an empty phrase is empty_value: "" and "   "', () => {    assert.deepEqual(codes('""'), ["empty_value"]);    assert.deepEqual(codes('"   "'), ["empty_value"]);  });});describe("parse: errors", () => {  test("unknown qualifiers are errors, assignee: included", () => {    for (const q of ["assignee:@me", "repo:a/b", "org:x", "milestone:v1", "in:title", "foo:bar", "http://example.com", "is-open:x"]) {      assert.deepEqual(codes(q), ["unknown_qualifier"], q);      assert.deepEqual(codes(q, "pulls"), ["unknown_qualifier"], q);    }  });  test("qualifiers of the other target are unsupported_qualifier", () => {    assert.deepEqual(codes("label:bug", "pulls"), ["unsupported_qualifier"]);    assert.deepEqual(codes("no:label", "pulls"), ["unsupported_qualifier"]);    assert.deepEqual(codes("review-requested:@me", "issues"), ["unsupported_qualifier"]);  });  test("an unknown key is reported before an empty value, and unsupported before empty", () => {    assert.deepEqual(codes("foo:"), ["unknown_qualifier"]);    assert.deepEqual(codes("label:", "pulls"), ["unsupported_qualifier"]);  });  test("empty values: label:, label:\"\", author:, is:", () => {    for (const q of ["label:", 'label:""', "author:", "is:", "created:"]) assert.deepEqual(codes(q), ["empty_value"], q);  });  test("negation is not allowed on no:, sort:, created:, updated: and #n (an empty value is reported first)", () => {    assert.deepEqual(codes("-sort:"), ["empty_value"]);    for (const q of ["-no:label", "-sort:created-asc", "-created:>2026-10-01", "-updated:<7d", "-#5"]) assert.deepEqual(codes(q), ["negation_not_allowed"], q);  });  test("negation_not_allowed comes before bad_value", () => {    assert.deepEqual(codes("-sort:bogus"), ["negation_not_allowed"]);  });  test("is: values depend on the target", () => {    for (const q of ["is:merged", "is:draft", "is:pr", "is:issue", "is:bogus"]) assert.deepEqual(codes(q), ["bad_value"], q);    for (const q of ["is:pr", "is:bogus", "is:unmerged"]) assert.deepEqual(codes(q, "pulls"), ["bad_value"], q);  });  test("sort: values depend on the target", () => {    assert.deepEqual(codes("sort:comments-desc", "pulls"), ["bad_value"]);    for (const q of ["sort:created", "sort:best-match", "sort:updated-down", "no:assignee"]) assert.deepEqual(codes(q), ["bad_value"], q);  });  test("labels follow the label rules: characters and 32 at most", () => {    for (const q of ["label:ñ", 'label:"a,b"', "label:-x", `label:${"a".repeat(33)}`]) assert.deepEqual(codes(q), ["bad_value"], q);    assert.equal(canon(`label:${"a".repeat(32)}`), `label:${"a".repeat(32)}`);    assert.equal(canon("label:v1.2_x-y"), "label:v1.2_x-y");  });  test("user references: @me, a UUID or an agent slug", () => {    for (const q of ["author:@bob", "author:bob_bot", "author:-x", "author:a.b", `author:${"a".repeat(40)}`]) assert.deepEqual(codes(q), ["bad_value"], q);    assert.equal(canon(`author:${"a".repeat(39)}`), `author:${"a".repeat(39)}`);  });  test("a second sort: with another value is a conflict on the second term", () => {    const e = failed("sort:created-asc is:open sort:updated-desc");    assert.deepEqual(e.map((x) => [x.code, x.start, x.end]), [["conflict", 25, 42]]);    assert.equal(canon("sort:created-asc sort:CREATED-ASC"), "sort:created-asc", "a repeated identical sort is no conflict");  });  test("error spans cover the whole term, the - included", () => {    assert.deepEqual(failed("is:open foo:bar").map((x) => [x.start, x.end]), [[8, 15]]);    assert.deepEqual(failed("-label: x").map((x) => [x.code, x.start, x.end]), [["empty_value", 0, 7]]);    assert.deepEqual(failed('x label:"bad,label"').map((x) => [x.code, x.start, x.end]), [["bad_value", 2, 19]]);  });  test("spans are UTF-16 offsets: an emoji before the term counts two", () => {    assert.deepEqual(failed("🐛 foo:bar").map((x) => [x.start, x.end]), [[3, 10]]);  });  test("every invalid term gets one error, in input order", () => {    assert.deepEqual(codes("foo:1 label: crash is:bogus -no:label"), ["unknown_qualifier", "empty_value", "bad_value", "negation_not_allowed"]);  });  test("a label value cannot carry SQL: it is a bad_value", () => {    assert.deepEqual(codes(`label:"x' OR 1=1 --"`), ["bad_value"]);  });});describe("parse: unicode and whitespace", () => {  test("no-break and ideographic spaces separate terms", () => {    assert.equal(canon("is:open label:bug crash x"), "is:open label:bug crash x");  });  test("unicode free text and emoji are kept exactly", () => {    assert.equal(canon("🐛 Ünïcode 日本語 ́e"), "🐛 Ünïcode 日本語 ́e");    assert.equal(canon("a\u0000b"), "a\u0000b", "a NUL is just text");  });});describe("parse: limits", () => {  test("1024 code units are allowed", () => {    const s = "a".repeat(1024);    assert.equal(canon(s), s);  });  test("1025 code units are too_long, alone, spanning the input", () => {    const s = `${"a".repeat(1019)} foo:x`;    assert.equal(s.length, 1025);    assert.deepEqual(failed(s).map((x) => [x.code, x.start, x.end]), [["too_long", 0, 1025]]);  });  test("an emoji counts as two code units against the limit", () => {    assert.deepEqual(codes("🐛".repeat(513)), ["too_long"]);    parsed("🐛".repeat(512));  });  test("a very long input is rejected quickly", () => {    const s = `"${"x ".repeat(100_000)}`;    const t = performance.now();    assert.deepEqual(codes(s), ["too_long"]);    assert.ok(performance.now() - t < 1000, "under a second");  });  test("32 terms are allowed; the 33rd is too_many_terms from its start to the end", () => {    const terms = Array.from({ length: 33 }, (_, i) => `w${i}`);    parsed(terms.slice(0, 32).join(" "));    const s = terms.join(" ");    const start = s.indexOf("w32");    assert.deepEqual(failed(s).map((x) => [x.code, x.start, x.end]), [["too_many_terms", start, s.length]]);  });  test("terms are counted as written: 33 copies of one term are too many, invalid ones included", () => {    assert.deepEqual(codes(Array(33).fill("is:open").join(" ")), ["too_many_terms"]);    assert.deepEqual(codes([...Array(32).fill("x"), "foo:bar"].join(" ")), ["too_many_terms"]);  });  test("many terms of every kind parse fast", () => {    const s = Array.from({ length: 32 }, (_, i) => ["label:bug", '"a b"', `#${i + 1}`, "-x", "created:>2026-10-01", "author:@me", "is:open", "zz"][i % 8]).join(" ");    const t = performance.now();    for (let i = 0; i < 200; i++) api.parseQuery(s, "issues");    assert.ok(performance.now() - t < 2000, "200 parses under 2 s");  });});