|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { canaryBucket, evaluateCanary, parseCanaryOverride, type CanaryInput } from "./canary.js"; |
3 | 3 | import { CANARIES, canaryEnvVar, findCanary, overdueCanaries } from "./canaryRegistry.js"; |
4 | | -import { CANARY_FEATURE_PREFIX, canaryFeatureKey, canaryFeatureProperties } from "./canary.js"; |
| 4 | +import { |
| 5 | + CANARY_FEATURE_PREFIX, |
| 6 | + canaryFeatureKey, |
| 7 | + canaryFeatureProperties, |
| 8 | + canaryReasonKey, |
| 9 | +} from "./canary.js"; |
5 | 10 |
|
6 | 11 | const base = (over: Partial<CanaryInput> = {}): CanaryInput => ({ |
7 | 12 | feature: "test-feature", |
@@ -371,3 +376,56 @@ describe("PostHog flag-shaped properties", () => { |
371 | 376 | expect(canaryFeatureProperties([])).toEqual({}); |
372 | 377 | }); |
373 | 378 | }); |
| 379 | + |
| 380 | +// The attribution property. Without it, an install reporting both "true" and |
| 381 | +// "false" for a canary whose percentage never moved is indistinguishable from |
| 382 | +// a developer toggling HF_CANARY_*. The first calibration read hit exactly |
| 383 | +// that: 304 installs reported both values and the anomalous ones could not be |
| 384 | +// separated from deliberate overrides. |
| 385 | +describe("canary reason property", () => { |
| 386 | + it("rides alongside the assignment, outside the $feature namespace", () => { |
| 387 | + const props = canaryFeatureProperties([ |
| 388 | + { name: "de-parallel-router", enabled: true, reason: "in_cohort" }, |
| 389 | + ]); |
| 390 | + expect(props["$feature/canary-de-parallel-router"]).toBe("true"); |
| 391 | + expect(props["canary_reason_de_parallel_router"]).toBe("in_cohort"); |
| 392 | + }); |
| 393 | + |
| 394 | + // A non-boolean under `$feature/` would corrupt the flag's own breakdowns, |
| 395 | + // which is the whole reason the reason gets its own key. |
| 396 | + it("never puts a reason inside the flag namespace", () => { |
| 397 | + const props = canaryFeatureProperties([{ name: "x", enabled: false, reason: "forced_off" }]); |
| 398 | + for (const [key, value] of Object.entries(props)) { |
| 399 | + if (key.startsWith(CANARY_FEATURE_PREFIX)) { |
| 400 | + expect(value).toMatch(/^(true|false)$/); |
| 401 | + } |
| 402 | + } |
| 403 | + }); |
| 404 | + |
| 405 | + it("separates a forced override from a genuine cohort roll at the same value", () => { |
| 406 | + const forced = canaryFeatureProperties([{ name: "f", enabled: true, reason: "forced_on" }]); |
| 407 | + const rolled = canaryFeatureProperties([{ name: "f", enabled: true, reason: "in_cohort" }]); |
| 408 | + // Identical assignment — only the reason tells them apart. This is the |
| 409 | + // distinction the calibration read could not make. |
| 410 | + expect(forced["$feature/canary-f"]).toBe(rolled["$feature/canary-f"]); |
| 411 | + expect(forced["canary_reason_f"]).not.toBe(rolled["canary_reason_f"]); |
| 412 | + }); |
| 413 | + |
| 414 | + it("emits `excluded` for CI, which replaces joining on is_ci", () => { |
| 415 | + const props = canaryFeatureProperties([{ name: "c", enabled: false, reason: "excluded" }]); |
| 416 | + // `excluded` and `out_of_cohort` are both enabled:false but mean different |
| 417 | + // things — CI was never bucketed, the other lost the roll. Counting them |
| 418 | + // together is what biased the first accuracy read low. |
| 419 | + expect(props["canary_reason_c"]).toBe("excluded"); |
| 420 | + }); |
| 421 | + |
| 422 | + it("omits the reason key when no reason is supplied", () => { |
| 423 | + const props = canaryFeatureProperties([{ name: "n", enabled: true }]); |
| 424 | + expect(props["$feature/canary-n"]).toBe("true"); |
| 425 | + expect(props).not.toHaveProperty("canary_reason_n"); |
| 426 | + }); |
| 427 | + |
| 428 | + it("sanitizes the name into a property-safe key", () => { |
| 429 | + expect(canaryReasonKey("de-parallel-router")).toBe("canary_reason_de_parallel_router"); |
| 430 | + }); |
| 431 | +}); |
0 commit comments