|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { meteringClaims, regionForMetering } from "./meteringClaims.server"; |
| 3 | + |
| 4 | +describe("regionForMetering", () => { |
| 5 | + it("returns the explicit region when set", () => { |
| 6 | + expect(regionForMetering("eu-central-1", "eu-central-1-microvm")).toBe("eu-central-1"); |
| 7 | + }); |
| 8 | + |
| 9 | + it("returns the worker queue when it has no colon", () => { |
| 10 | + expect(regionForMetering(null, "us-nyc-3")).toBe("us-nyc-3"); |
| 11 | + }); |
| 12 | + |
| 13 | + it("returns the worker queue prefix before the first colon", () => { |
| 14 | + expect(regionForMetering(undefined, "us-nyc-3:scheduled")).toBe("us-nyc-3"); |
| 15 | + expect(regionForMetering(undefined, "us-nyc-3:a:b")).toBe("us-nyc-3"); |
| 16 | + }); |
| 17 | + |
| 18 | + it("falls back to the worker queue when the region is empty", () => { |
| 19 | + expect(regionForMetering("", "us-nyc-3")).toBe("us-nyc-3"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("returns undefined when neither is known", () => { |
| 23 | + expect(regionForMetering(null, null)).toBeUndefined(); |
| 24 | + expect(regionForMetering(undefined, "")).toBeUndefined(); |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +describe("meteringClaims", () => { |
| 29 | + it("lowercases the environment type", () => { |
| 30 | + expect(meteringClaims({ environmentType: "PRODUCTION", region: "us-east-1" })).toEqual({ |
| 31 | + environment_type: "production", |
| 32 | + region: "us-east-1", |
| 33 | + }); |
| 34 | + expect(meteringClaims({ environmentType: "STAGING" }).environment_type).toBe("staging"); |
| 35 | + expect(meteringClaims({ environmentType: "PREVIEW" }).environment_type).toBe("preview"); |
| 36 | + expect(meteringClaims({ environmentType: "DEVELOPMENT" }).environment_type).toBe("development"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("prefers the run's region over its worker queue", () => { |
| 40 | + expect( |
| 41 | + meteringClaims({ |
| 42 | + environmentType: "PRODUCTION", |
| 43 | + region: "eu-central-1", |
| 44 | + workerQueue: "eu-central-1-microvm", |
| 45 | + }) |
| 46 | + ).toEqual({ environment_type: "production", region: "eu-central-1" }); |
| 47 | + }); |
| 48 | + |
| 49 | + it("falls back to the worker queue prefix before the first colon", () => { |
| 50 | + expect( |
| 51 | + meteringClaims({ |
| 52 | + environmentType: "PRODUCTION", |
| 53 | + region: null, |
| 54 | + workerQueue: "us-nyc-3:scheduled", |
| 55 | + }) |
| 56 | + ).toEqual({ environment_type: "production", region: "us-nyc-3" }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("treats an empty region as unknown", () => { |
| 60 | + expect( |
| 61 | + meteringClaims({ environmentType: "STAGING", region: "", workerQueue: "us-nyc-3" }) |
| 62 | + ).toEqual({ environment_type: "staging", region: "us-nyc-3" }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("omits region when neither region nor worker queue is known", () => { |
| 66 | + const claims = meteringClaims({ environmentType: "PRODUCTION", region: null, workerQueue: "" }); |
| 67 | + |
| 68 | + expect(claims).toEqual({ environment_type: "production" }); |
| 69 | + expect("region" in claims).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it("always reports development runs as local", () => { |
| 73 | + expect( |
| 74 | + meteringClaims({ |
| 75 | + environmentType: "DEVELOPMENT", |
| 76 | + region: "cm_dev_environment_id", |
| 77 | + workerQueue: "cm_dev_environment_id", |
| 78 | + }) |
| 79 | + ).toEqual({ environment_type: "development", region: "local" }); |
| 80 | + expect(meteringClaims({ environmentType: "DEVELOPMENT" })).toEqual({ |
| 81 | + environment_type: "development", |
| 82 | + region: "local", |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments