diff --git a/src/reporters/github/github.test.ts b/src/reporters/github/github.test.ts index c676fd3..07fa0b5 100644 --- a/src/reporters/github/github.test.ts +++ b/src/reporters/github/github.test.ts @@ -1,8 +1,29 @@ import { test, expect, describe } from "vitest"; +import { readFileSync } from "node:fs"; +import { fileURLToPath } from "node:url"; +import { dirname, join } from "node:path"; +import n from "nunjucks"; import { formatCost, queryPreview, buildViewModel } from "./github.ts"; -import type { ReportContext } from "../reporter.ts"; +import { isQueryLong, renderExplain, type ReportContext } from "../reporter.ts"; import type { RunComparison } from "../site-api.ts"; +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const successTemplate = readFileSync(join(__dirname, "success.md.j2"), "utf-8"); + +n.configure({ autoescape: false, trimBlocks: true, lstripBlocks: true }); + +function renderTemplate(ctx: ReportContext) { + const viewModel = buildViewModel(ctx); + return n.renderString(successTemplate, { + ...ctx, + ...viewModel, + isQueryLong, + renderExplain, + formatCost, + }); +} + describe("formatCost", () => { test("formats small numbers without commas", () => { expect(formatCost(9)).toBe("9"); @@ -300,3 +321,22 @@ describe("buildViewModel", () => { }); }); + +describe("template rendering", () => { + test("renders queryStats.total as the query count", () => { + const ctx = makeContext({ + queryStats: { total: 5, matched: 3, optimized: 1, errored: 0 }, + comparison: makeComparison(), + }); + const output = renderTemplate(ctx); + expect(output).toContain("5 queries analyzed"); + }); + + test("renders queryStats.total in no-comparison mode", () => { + const ctx = makeContext({ + queryStats: { total: 3, matched: 1, optimized: 0, errored: 0 }, + }); + const output = renderTemplate(ctx); + expect(output).toContain("3 queries analyzed"); + }); +}); diff --git a/src/reporters/reporter.ts b/src/reporters/reporter.ts index 165296f..8e55315 100644 --- a/src/reporters/reporter.ts +++ b/src/reporters/reporter.ts @@ -62,7 +62,7 @@ export type ReportMetadata = { declare const s: unique symbol; export interface ReportStatistics { - /** Total number of queries seen in the log */ + /** Number of unique, non-filtered queries analyzed */ total: number; /** Number of queries that matched the query pattern */ matched: number; diff --git a/src/runner.ts b/src/runner.ts index 44f5bd6..a48f15a 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -108,7 +108,6 @@ export class Runner { if (loglevel !== "LOG" || !queryString.startsWith("plan:")) { continue; } - total++; const planString: string = queryString.split("plan:")[1].trim(); const json = preprocessEncodedJson(planString); if (!json) { @@ -135,6 +134,7 @@ export class Runner { continue; } + total++; const recentQuery = await RecentQuery.fromLogEntry(query, hash); recentQueries.push(recentQuery) }