|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { operatorQuoteSchema, searchQuerySchema } from "./schema.input"; |
| 4 | + |
| 5 | +describe("operatorQuoteSchema", () => { |
| 6 | + it("should pass when quoted operators have quotes", () => { |
| 7 | + const validQueries = [ |
| 8 | + 'title:"hello world"', |
| 9 | + 'body:"some description"', |
| 10 | + 'label:"bug"', |
| 11 | + 'collection:"my collection"', |
| 12 | + // Mixed with unquoted operators |
| 13 | + 'title:"hello" author:coder', |
| 14 | + 'state:open body:"detailed explanation"', |
| 15 | + // Multiple quoted operators |
| 16 | + 'title:"hello" body:"world"', |
| 17 | + // With other content |
| 18 | + 'title:"hello world" some other text', |
| 19 | + ]; |
| 20 | + |
| 21 | + validQueries.forEach((query) => { |
| 22 | + expect(() => operatorQuoteSchema.parse(query)).not.toThrow(); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should fail when quoted operators lack quotes", () => { |
| 27 | + const invalidQueries = [ |
| 28 | + "title:hello world", |
| 29 | + "body:some description", |
| 30 | + "label:bug", |
| 31 | + "collection:my collection", |
| 32 | + // Mixed cases |
| 33 | + 'title:"valid" body:invalid', |
| 34 | + 'title:invalid body:"valid"', |
| 35 | + // With other content |
| 36 | + "title:hello some other text", |
| 37 | + ]; |
| 38 | + |
| 39 | + invalidQueries.forEach((query) => { |
| 40 | + expect(() => operatorQuoteSchema.parse(query)).toThrow(/requires quotes/); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should pass when unquoted operators are used without quotes", () => { |
| 45 | + const validQueries = [ |
| 46 | + "author:coder", |
| 47 | + "state:open", |
| 48 | + "repo:semhub", |
| 49 | + "org:coder", |
| 50 | + // Mixed with quoted operators |
| 51 | + 'author:coder title:"hello"', |
| 52 | + // Multiple unquoted operators |
| 53 | + "state:open author:coder", |
| 54 | + ]; |
| 55 | + |
| 56 | + validQueries.forEach((query) => { |
| 57 | + expect(() => operatorQuoteSchema.parse(query)).not.toThrow(); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("searchQuerySchema", () => { |
| 63 | + describe("multiple operator validation", () => { |
| 64 | + it("should fail when multiple instances of unique operators are used", () => { |
| 65 | + const invalidQueries = [ |
| 66 | + "state:open state:closed", |
| 67 | + "repo:a repo:b", |
| 68 | + "author:x author:y", |
| 69 | + "org:a org:b", |
| 70 | + ]; |
| 71 | + |
| 72 | + invalidQueries.forEach((query) => { |
| 73 | + expect(() => searchQuerySchema.parse(query)).toThrow(/more than one/); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("empty query validation", () => { |
| 79 | + it("should fail when no substantive query is provided", () => { |
| 80 | + const emptyQueries = [ |
| 81 | + "", |
| 82 | + " ", |
| 83 | + "state:open", // only filter, no search content |
| 84 | + ]; |
| 85 | + |
| 86 | + emptyQueries.forEach((query) => { |
| 87 | + expect(() => searchQuerySchema.parse(query)).toThrow( |
| 88 | + /no substantive query/, |
| 89 | + ); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should pass when substantive query is provided", () => { |
| 94 | + const validQueries = [ |
| 95 | + "hello world", |
| 96 | + 'title:"hello"', |
| 97 | + 'body:"description"', |
| 98 | + "state:open hello", |
| 99 | + '"exact match"', |
| 100 | + ]; |
| 101 | + |
| 102 | + validQueries.forEach((query) => { |
| 103 | + expect(() => searchQuerySchema.parse(query)).not.toThrow(); |
| 104 | + }); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments