|
1 | 1 | import { test, expect, describe } from "vitest"; |
2 | 2 | import { |
| 3 | + buildQueries, |
3 | 4 | compareRuns, |
4 | 5 | type CiQueryPayload, |
5 | 6 | type PreviousRun, |
6 | 7 | } from "./site-api.ts"; |
| 8 | +import type { QueryProcessResult } from "../runner.ts"; |
7 | 9 |
|
8 | 10 | function makeQuery(hash: string, cost: number = 100): CiQueryPayload { |
9 | 11 | return { |
@@ -31,6 +33,99 @@ function makePreviousRun(queries: CiQueryPayload[]): PreviousRun { |
31 | 33 | }; |
32 | 34 | } |
33 | 35 |
|
| 36 | +describe("buildQueries", () => { |
| 37 | + test("filters out invalid results", () => { |
| 38 | + const results: QueryProcessResult[] = [ |
| 39 | + { |
| 40 | + kind: "no_improvement", |
| 41 | + fingerprint: "hash-a", |
| 42 | + rawQuery: "SELECT 1", |
| 43 | + formattedQuery: "SELECT 1", |
| 44 | + cost: 10, |
| 45 | + existingIndexes: [], |
| 46 | + nudges: [], |
| 47 | + tags: [], |
| 48 | + referencedTables: [], |
| 49 | + }, |
| 50 | + { kind: "invalid" }, |
| 51 | + { |
| 52 | + kind: "no_improvement", |
| 53 | + fingerprint: "hash-b", |
| 54 | + rawQuery: "SELECT 2", |
| 55 | + formattedQuery: "SELECT 2", |
| 56 | + cost: 20, |
| 57 | + existingIndexes: [], |
| 58 | + nudges: [], |
| 59 | + tags: [], |
| 60 | + referencedTables: [], |
| 61 | + }, |
| 62 | + ]; |
| 63 | + |
| 64 | + const queries = buildQueries(results); |
| 65 | + expect(queries).toHaveLength(2); |
| 66 | + expect(queries.map((q) => q.hash)).toEqual(["hash-a", "hash-b"]); |
| 67 | + }); |
| 68 | + |
| 69 | + test("filters out ignored query hashes", () => { |
| 70 | + const results: QueryProcessResult[] = [ |
| 71 | + { |
| 72 | + kind: "no_improvement", |
| 73 | + fingerprint: "hash-a", |
| 74 | + rawQuery: "SELECT 1", |
| 75 | + formattedQuery: "SELECT 1", |
| 76 | + cost: 10, |
| 77 | + existingIndexes: [], |
| 78 | + nudges: [], |
| 79 | + tags: [], |
| 80 | + referencedTables: [], |
| 81 | + }, |
| 82 | + { |
| 83 | + kind: "no_improvement", |
| 84 | + fingerprint: "hash-b", |
| 85 | + rawQuery: "SELECT 2", |
| 86 | + formattedQuery: "SELECT 2", |
| 87 | + cost: 20, |
| 88 | + existingIndexes: [], |
| 89 | + nudges: [], |
| 90 | + tags: [], |
| 91 | + referencedTables: [], |
| 92 | + }, |
| 93 | + ]; |
| 94 | + |
| 95 | + const queries = buildQueries(results, { |
| 96 | + ignoredQueryHashes: ["hash-a"], |
| 97 | + acknowledgedQueryHashes: [], |
| 98 | + regressionThreshold: 10, |
| 99 | + minimumCost: 0, |
| 100 | + }); |
| 101 | + expect(queries).toHaveLength(1); |
| 102 | + expect(queries[0].hash).toBe("hash-b"); |
| 103 | + }); |
| 104 | + |
| 105 | + test("count reflects deduplicated output, not raw input length", () => { |
| 106 | + const results: QueryProcessResult[] = [ |
| 107 | + { |
| 108 | + kind: "no_improvement", |
| 109 | + fingerprint: "hash-a", |
| 110 | + rawQuery: "SELECT 1", |
| 111 | + formattedQuery: "SELECT 1", |
| 112 | + cost: 10, |
| 113 | + existingIndexes: [], |
| 114 | + nudges: [], |
| 115 | + tags: [], |
| 116 | + referencedTables: [], |
| 117 | + }, |
| 118 | + { kind: "invalid" }, |
| 119 | + { kind: "invalid" }, |
| 120 | + { kind: "invalid" }, |
| 121 | + ]; |
| 122 | + |
| 123 | + const queries = buildQueries(results); |
| 124 | + // 4 results in, but only 1 valid query out |
| 125 | + expect(queries).toHaveLength(1); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
34 | 129 | describe("compareRuns", () => { |
35 | 130 | describe("new query detection via previousRun", () => { |
36 | 131 | test("when previousRun has no queries, all current queries are new", () => { |
|
0 commit comments