import { test, expect } from "bun:test"; import { publicGrafanaEndpoint, metricFrameValues } from "../scripts/grafana"; import { assertOriginalGrafanaRules, isOwnHistoryWrite, } from "../scripts/grafana"; const endpoint = () => ({ Config: { Labels: { "com.docker.compose.project": "mtuci-vp7-security", "com.docker.compose.service": "grafana-gateway", }, }, State: { Running: true }, NetworkSettings: { Networks: { private: {}, edge: {} }, Ports: { "3443/tcp": [{ HostIp: "127.0.0.1", HostPort: "18447" }] }, }, }); test("Grafana endpoint uses the verified published loopback binding despite two networks", () => { expect(publicGrafanaEndpoint(endpoint())).toEqual({ host: "127.0.0.1", port: 18447, }); }); test("Grafana endpoint rejects foreign, stopped, wildcard, missing and ambiguous bindings", () => { for (const change of [ (x: any) => (x.Config.Labels["com.docker.compose.project"] = "foreign"), (x: any) => (x.Config.Labels["com.docker.compose.service"] = "lb"), (x: any) => (x.State.Running = false), (x: any) => (x.NetworkSettings.Ports = null), (x: any) => x.NetworkSettings.Ports["3443/tcp"].push({ HostIp: "127.0.0.1", HostPort: "18447", }), (x: any) => (x.NetworkSettings.Ports["3443/tcp"][0].HostIp = "0.0.0.0"), (x: any) => (x.NetworkSettings.Ports["3443/tcp"][0].HostPort = "65536"), ]) { const x = endpoint(); change(x); expect(() => publicGrafanaEndpoint(x)).toThrow(); } }); const frames = (value: any) => ({ results: { A: { status: 200, frames: [ { schema: { fields: [ { name: "Time", type: "time" }, { name: "Value", type: "number" }, ], }, data: { values: [[123], [value]] }, }, ], }, }, }); test("real numeric zero is a valid metric sample", () => { expect(metricFrameValues(frames(0))).toEqual([0]); }); test("HTTP200 with datasource error, empty, null, nonfinite or ragged values cannot pass", () => { for (const sample of [ frames(null), frames(NaN), frames(Infinity), frames("0"), { results: { A: { status: 200, error: "query failed", frames: [] } } }, { results: { A: { status: 200, frames: [] } } }, { results: { A: { status: 500, frames: frames(1).results.A.frames } } }, { results: { A: { status: 200, frames: [ { schema: { fields: [{ type: "number" }] }, data: { values: [[]] }, }, ], }, }, }, ]) expect(() => metricFrameValues(sample)).toThrow(); }); test("native history first creation binds exact collection and user key", () => { const collection = "/apis/userstorage.grafana.app/v0alpha1/namespaces/default/user-storage"; const name = "grafana-page-history:fg4fpuerxx79cb"; expect( isOwnHistoryWrite("POST", collection + "/", { metadata: { name } }, name), ).toBe(true); expect( isOwnHistoryWrite( "POST", collection + "/foreign", { metadata: { name } }, name, ), ).toBe(false); }); test("existing native history uses versioned PATCH at the same owned key", () => { const collection = "/apis/userstorage.grafana.app/v0alpha1/namespaces/default/user-storage", name = "grafana-page-history:fg4fpuerxx79cb"; const body = { metadata: { resourceVersion: "123" }, spec: { data: { history: "fixture" } }, }; expect(isOwnHistoryWrite("PATCH", collection + "/" + name, body, name)).toBe( true, ); expect( isOwnHistoryWrite( "PATCH", collection + "/grafana-page-history:other", body, name, ), ).toBe(false); }); test("native Grafana evaluator key reordering does not change a rule", () => { const original: any = { uid: "vp7-server-errors", title: "Error ratio", data: [ { model: { expr: "actual_metric" } }, {}, { model: { conditions: [{ evaluator: { type: "gt", params: [0.05] } }] }, }, ], }; const actual = structuredClone(original); actual.data[2].model.conditions[0].evaluator = { params: [0.05], type: "gt" }; expect(() => assertOriginalGrafanaRules([actual], [original])).not.toThrow(); actual.data[2].model.conditions[0].evaluator.params = [0.5]; expect(() => assertOriginalGrafanaRules([actual], [original])).toThrow( "GRAFANA_INCIDENT_RULE_CHANGED", ); });