|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { metricSourceSchema } from "./metric-source"; |
| 3 | + |
| 4 | +describe("metricSourceSchema", () => { |
| 5 | + test("accepts a minimal configuration and defaults executor to app_service_principal", () => { |
| 6 | + const config = { |
| 7 | + $schema: |
| 8 | + "https://databricks.github.io/appkit/schemas/metric-source.schema.json", |
| 9 | + metricViews: { |
| 10 | + revenue: { source: "appkit_demo.public.revenue_metrics" }, |
| 11 | + }, |
| 12 | + }; |
| 13 | + const result = metricSourceSchema.safeParse(config); |
| 14 | + expect(result.success).toBe(true); |
| 15 | + expect(result.data?.metricViews?.revenue.executor).toBe( |
| 16 | + "app_service_principal", |
| 17 | + ); |
| 18 | + }); |
| 19 | + |
| 20 | + test("accepts explicit executor values", () => { |
| 21 | + const config = { |
| 22 | + metricViews: { |
| 23 | + revenue: { |
| 24 | + source: "demo.public.revenue", |
| 25 | + executor: "app_service_principal", |
| 26 | + }, |
| 27 | + my_orders: { source: "main.sales.orders_by_user", executor: "user" }, |
| 28 | + }, |
| 29 | + }; |
| 30 | + const result = metricSourceSchema.safeParse(config); |
| 31 | + expect(result.success).toBe(true); |
| 32 | + expect(result.data?.metricViews?.my_orders.executor).toBe("user"); |
| 33 | + }); |
| 34 | + |
| 35 | + test("accepts an empty configuration", () => { |
| 36 | + expect(metricSourceSchema.safeParse({}).success).toBe(true); |
| 37 | + expect(metricSourceSchema.safeParse({ metricViews: {} }).success).toBe( |
| 38 | + true, |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + test("accepts metric keys with underscores", () => { |
| 43 | + const config = { |
| 44 | + metricViews: { |
| 45 | + customer_metrics: { source: "demo.public.customer_metrics" }, |
| 46 | + }, |
| 47 | + }; |
| 48 | + expect(metricSourceSchema.safeParse(config).success).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + test("rejects the legacy sp/obo lane shape", () => { |
| 52 | + const config = { |
| 53 | + sp: { revenue: { source: "demo.public.revenue" } }, |
| 54 | + obo: { my_orders: { source: "main.sales.orders_by_user" } }, |
| 55 | + }; |
| 56 | + expect(metricSourceSchema.safeParse(config).success).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + test("rejects invalid executor values", () => { |
| 60 | + for (const executor of ["sp", "obo", "service_principal", "USER"]) { |
| 61 | + const config = { |
| 62 | + metricViews: { revenue: { source: "a.b.c", executor } }, |
| 63 | + }; |
| 64 | + expect(metricSourceSchema.safeParse(config).success).toBe(false); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + test("rejects a bare-string entry (must be an object)", () => { |
| 69 | + const config = { |
| 70 | + metricViews: { revenue: "demo.public.revenue" }, |
| 71 | + }; |
| 72 | + expect(metricSourceSchema.safeParse(config).success).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + test("rejects an entry without source", () => { |
| 76 | + const config = { |
| 77 | + metricViews: { revenue: {} }, |
| 78 | + }; |
| 79 | + expect(metricSourceSchema.safeParse(config).success).toBe(false); |
| 80 | + }); |
| 81 | + |
| 82 | + test("rejects unknown fields on entries", () => { |
| 83 | + const config = { |
| 84 | + metricViews: { |
| 85 | + revenue: { |
| 86 | + source: "a.b.c", |
| 87 | + ttl: 5, // future option, not in v1 |
| 88 | + }, |
| 89 | + }, |
| 90 | + }; |
| 91 | + expect(metricSourceSchema.safeParse(config).success).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + test("rejects unknown top-level keys", () => { |
| 95 | + expect(metricSourceSchema.safeParse({ foo: 1 }).success).toBe(false); |
| 96 | + expect( |
| 97 | + metricSourceSchema.safeParse({ metricViews: {}, unknown: {} }).success, |
| 98 | + ).toBe(false); |
| 99 | + }); |
| 100 | + |
| 101 | + test("rejects metric keys that start with a digit", () => { |
| 102 | + const config = { |
| 103 | + metricViews: { "1bad": { source: "a.b.c" } }, |
| 104 | + }; |
| 105 | + expect(metricSourceSchema.safeParse(config).success).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + test("rejects metric keys containing a hyphen", () => { |
| 109 | + const config = { |
| 110 | + metricViews: { "bad-key": { source: "a.b.c" } }, |
| 111 | + }; |
| 112 | + expect(metricSourceSchema.safeParse(config).success).toBe(false); |
| 113 | + }); |
| 114 | + |
| 115 | + test("rejects a non-three-part FQN", () => { |
| 116 | + const cases = [ |
| 117 | + "revenue", // single token |
| 118 | + "demo.revenue", // two parts |
| 119 | + "four.parts.really.bad", |
| 120 | + ".starts.with.dot", |
| 121 | + "ends.with.dot.", |
| 122 | + ]; |
| 123 | + for (const source of cases) { |
| 124 | + const config = { metricViews: { revenue: { source } } }; |
| 125 | + expect(metricSourceSchema.safeParse(config).success).toBe(false); |
| 126 | + } |
| 127 | + }); |
| 128 | +}); |
0 commit comments