From 33cb36c083c1ae8b0306990bb97f238aa2c6d2e4 Mon Sep 17 00:00:00 2001 From: Saiichi Hashimoto Date: Mon, 23 Dec 2024 12:20:56 -0600 Subject: [PATCH] fix(groq): using boolean comparisons no longer breaks queries --- packages/groq/src/internal.ts | 6 +- packages/groq/src/operators.test.ts | 264 ++++++++++++++++++++++++++++ 2 files changed, 269 insertions(+), 1 deletion(-) diff --git a/packages/groq/src/internal.ts b/packages/groq/src/internal.ts index edb4ebc4..b9d2e235 100644 --- a/packages/groq/src/internal.ts +++ b/packages/groq/src/internal.ts @@ -1844,7 +1844,11 @@ type EvaluateBooleanOperator< stronger: infer TStronger; weaker: infer TWeaker; } - ? Evaluate extends TStronger + ? boolean extends Evaluate + ? Evaluate + : boolean extends Evaluate + ? Evaluate + : Evaluate extends TStronger ? TStronger : Evaluate extends TStronger ? TStronger diff --git a/packages/groq/src/operators.test.ts b/packages/groq/src/operators.test.ts index c0a00d10..7a086adc 100644 --- a/packages/groq/src/operators.test.ts +++ b/packages/groq/src/operators.test.ts @@ -113,6 +113,138 @@ describe("operators", () => { >(); }); + it("$param&&true (boolean)", async () => { + const query = "$param&&true"; + + const tree = parse(query); + + const expectedTree = { + left: { name: "param", type: "Parameter" }, + right: { type: "Value", value: true }, + type: "And", + } as const; + + expect(tree).toStrictEqual(expectedTree); + expectType>().toStrictEqual< + WritableDeep + >(); + + const params = { param: true as boolean } as const; + + const result = await (await evaluate(tree, { params })).get(); + + const expectedResult = true; + + expect(result).toStrictEqual(expectedResult); + expectType< + ExecuteQuery< + typeof query, + ScopeFromPartialContext<{ + parameters: WritableDeep; + }> + > + >().toStrictEqual(); + }); + + it("$param&&false (boolean)", async () => { + const query = "$param&&false"; + + const tree = parse(query); + + const expectedTree = { + left: { name: "param", type: "Parameter" }, + right: { type: "Value", value: false }, + type: "And", + } as const; + + expect(tree).toStrictEqual(expectedTree); + expectType>().toStrictEqual< + WritableDeep + >(); + + const params = { param: true as boolean } as const; + + const result = await (await evaluate(tree, { params })).get(); + + const expectedResult = false; + + expect(result).toStrictEqual(expectedResult); + expectType< + ExecuteQuery< + typeof query, + ScopeFromPartialContext<{ + parameters: WritableDeep; + }> + > + >().toStrictEqual(); + }); + + it("true&&$param (boolean)", async () => { + const query = "true&&$param"; + + const tree = parse(query); + + const expectedTree = { + left: { type: "Value", value: true }, + right: { name: "param", type: "Parameter" }, + type: "And", + } as const; + + expect(tree).toStrictEqual(expectedTree); + expectType>().toStrictEqual< + WritableDeep + >(); + + const params = { param: true as boolean } as const; + + const result = await (await evaluate(tree, { params })).get(); + + const expectedResult = true; + + expect(result).toStrictEqual(expectedResult); + expectType< + ExecuteQuery< + typeof query, + ScopeFromPartialContext<{ + parameters: WritableDeep; + }> + > + >().toStrictEqual(); + }); + + it("false&&$param (boolean)", async () => { + const query = "false&&$param"; + + const tree = parse(query); + + const expectedTree = { + left: { type: "Value", value: false }, + right: { name: "param", type: "Parameter" }, + type: "And", + } as const; + + expect(tree).toStrictEqual(expectedTree); + expectType>().toStrictEqual< + WritableDeep + >(); + + const params = { param: true as boolean } as const; + + const result = await (await evaluate(tree)).get(); + + const expectedResult = false; + + expect(result).toStrictEqual(expectedResult); + expectType< + ExecuteQuery< + typeof query, + ScopeFromPartialContext<{ + parameters: WritableDeep; + }> + > + >().toStrictEqual(); + }); + it('""&&false', async () => { const query = '""&&false'; @@ -349,6 +481,138 @@ describe("operators", () => { >(); }); + it("$param||true (boolean)", async () => { + const query = "$param||true"; + + const tree = parse(query); + + const expectedTree = { + left: { name: "param", type: "Parameter" }, + right: { type: "Value", value: true }, + type: "Or", + } as const; + + expect(tree).toStrictEqual(expectedTree); + expectType>().toStrictEqual< + WritableDeep + >(); + + const params = { param: true as boolean } as const; + + const result = await (await evaluate(tree, { params })).get(); + + const expectedResult = true; + + expect(result).toStrictEqual(expectedResult); + expectType< + ExecuteQuery< + typeof query, + ScopeFromPartialContext<{ + parameters: WritableDeep; + }> + > + >().toStrictEqual(); + }); + + it("$param||false (boolean)", async () => { + const query = "$param||false"; + + const tree = parse(query); + + const expectedTree = { + left: { name: "param", type: "Parameter" }, + right: { type: "Value", value: false }, + type: "Or", + } as const; + + expect(tree).toStrictEqual(expectedTree); + expectType>().toStrictEqual< + WritableDeep + >(); + + const params = { param: true as boolean } as const; + + const result = await (await evaluate(tree, { params })).get(); + + const expectedResult = false; + + expect(result).toStrictEqual(expectedResult); + expectType< + ExecuteQuery< + typeof query, + ScopeFromPartialContext<{ + parameters: WritableDeep; + }> + > + >().toStrictEqual(); + }); + + it("true||$param (boolean)", async () => { + const query = "true||$param"; + + const tree = parse(query); + + const expectedTree = { + left: { type: "Value", value: true }, + right: { name: "param", type: "Parameter" }, + type: "Or", + } as const; + + expect(tree).toStrictEqual(expectedTree); + expectType>().toStrictEqual< + WritableDeep + >(); + + const params = { param: true as boolean } as const; + + const result = await (await evaluate(tree, { params })).get(); + + const expectedResult = true; + + expect(result).toStrictEqual(expectedResult); + expectType< + ExecuteQuery< + typeof query, + ScopeFromPartialContext<{ + parameters: WritableDeep; + }> + > + >().toStrictEqual(); + }); + + it("false||$param (boolean)", async () => { + const query = "false||$param"; + + const tree = parse(query); + + const expectedTree = { + left: { type: "Value", value: false }, + right: { name: "param", type: "Parameter" }, + type: "Or", + } as const; + + expect(tree).toStrictEqual(expectedTree); + expectType>().toStrictEqual< + WritableDeep + >(); + + const params = { param: true as boolean } as const; + + const result = await (await evaluate(tree)).get(); + + const expectedResult = false; + + expect(result).toStrictEqual(expectedResult); + expectType< + ExecuteQuery< + typeof query, + ScopeFromPartialContext<{ + parameters: WritableDeep; + }> + > + >().toStrictEqual(); + }); + it('""||true', async () => { const query = '""||true';