|
| 1 | +import { expect } from "chai"; |
| 2 | +import { remoteConfigApiOrigin } from "../api"; |
| 3 | +import * as nock from "nock"; |
| 4 | +import * as Table from "cli-table3"; |
| 5 | +import * as util from "util"; |
| 6 | + |
| 7 | +import * as rcExperiment from "./getexperiment"; |
| 8 | +import { GetExperimentResult, NAMESPACE_FIREBASE } from "./interfaces"; |
| 9 | +import { FirebaseError } from "../error"; |
| 10 | + |
| 11 | +const PROJECT_ID = "1234567890"; |
| 12 | +const EXPERIMENT_ID_1 = "1"; |
| 13 | +const EXPERIMENT_ID_2 = "2"; |
| 14 | + |
| 15 | +// Test sample experiment |
| 16 | +const expectedExperimentResult: GetExperimentResult = { |
| 17 | + name: "projects/1234567890/namespaces/firebase/experiments/1", |
| 18 | + definition: { |
| 19 | + displayName: "param_one", |
| 20 | + service: "EXPERIMENT_SERVICE_REMOTE_CONFIG", |
| 21 | + objectives: { |
| 22 | + activationEvent: {}, |
| 23 | + eventObjectives: [ |
| 24 | + { |
| 25 | + isPrimary: true, |
| 26 | + systemObjectiveDetails: { |
| 27 | + objective: "total_revenue", |
| 28 | + }, |
| 29 | + }, |
| 30 | + { |
| 31 | + systemObjectiveDetails: { |
| 32 | + objective: "retention_7", |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + customObjectiveDetails: { |
| 37 | + event: "app_exception", |
| 38 | + countType: "NO_EVENT_USERS", |
| 39 | + }, |
| 40 | + }, |
| 41 | + ], |
| 42 | + }, |
| 43 | + variants: [ |
| 44 | + { |
| 45 | + name: "Baseline", |
| 46 | + weight: 1, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "Variant A", |
| 50 | + weight: 1, |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + state: "PENDING", |
| 55 | + startTime: "1970-01-01T00:00:00Z", |
| 56 | + endTime: "1970-01-01T00:00:00Z", |
| 57 | + lastUpdateTime: "2025-07-25T08:24:30.682Z", |
| 58 | + etag: "e1", |
| 59 | +}; |
| 60 | + |
| 61 | +describe("Remote Config Experiment", () => { |
| 62 | + describe("getExperiment", () => { |
| 63 | + afterEach(() => { |
| 64 | + expect(nock.isDone()).to.equal(true, "all nock stubs should have been called"); |
| 65 | + nock.cleanAll(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should successfully retrieve a Remote Config experiment by ID", async () => { |
| 69 | + nock(remoteConfigApiOrigin()) |
| 70 | + .get(`/v1/projects/${PROJECT_ID}/namespaces/firebase/experiments/${EXPERIMENT_ID_1}`) |
| 71 | + .reply(200, expectedExperimentResult); |
| 72 | + |
| 73 | + const experimentOne = await rcExperiment.getExperiment( |
| 74 | + PROJECT_ID, |
| 75 | + NAMESPACE_FIREBASE, |
| 76 | + EXPERIMENT_ID_1, |
| 77 | + ); |
| 78 | + expect(experimentOne).to.deep.equal(expectedExperimentResult); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should reject with a FirebaseError if the API call fails", async () => { |
| 82 | + nock(remoteConfigApiOrigin()) |
| 83 | + .get(`/v1/projects/${PROJECT_ID}/namespaces/firebase/experiments/${EXPERIMENT_ID_2}`) |
| 84 | + .reply(404, {}); |
| 85 | + |
| 86 | + await expect( |
| 87 | + rcExperiment.getExperiment(PROJECT_ID, NAMESPACE_FIREBASE, EXPERIMENT_ID_2), |
| 88 | + ).to.eventually.be.rejectedWith( |
| 89 | + FirebaseError, |
| 90 | + `Failed to get Remote Config experiment with ID 2 for project 1234567890.`, |
| 91 | + ); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe("parseExperiment", () => { |
| 96 | + it("should correctly parse and format an experiment result into a tabular format", () => { |
| 97 | + const resultTable = rcExperiment.parseExperiment(expectedExperimentResult); |
| 98 | + |
| 99 | + const expectedTable = [ |
| 100 | + ["Name", expectedExperimentResult.name], |
| 101 | + ["Display Name", expectedExperimentResult.definition.displayName], |
| 102 | + ["Service", expectedExperimentResult.definition.service], |
| 103 | + [ |
| 104 | + "Objectives", |
| 105 | + util.inspect(expectedExperimentResult.definition.objectives, { |
| 106 | + showHidden: false, |
| 107 | + depth: null, |
| 108 | + }), |
| 109 | + ], |
| 110 | + [ |
| 111 | + "Variants", |
| 112 | + util.inspect(expectedExperimentResult.definition.variants, { |
| 113 | + showHidden: false, |
| 114 | + depth: null, |
| 115 | + }), |
| 116 | + ], |
| 117 | + ["State", expectedExperimentResult.state], |
| 118 | + ["Start Time", expectedExperimentResult.startTime], |
| 119 | + ["End Time", expectedExperimentResult.endTime], |
| 120 | + ["Last Update Time", expectedExperimentResult.lastUpdateTime], |
| 121 | + ["etag", expectedExperimentResult.etag], |
| 122 | + ]; |
| 123 | + |
| 124 | + const expectedTableString = new Table({ |
| 125 | + head: ["Entry Name", "Value"], |
| 126 | + style: { head: ["green"] }, |
| 127 | + }); |
| 128 | + expectedTableString.push(...expectedTable); |
| 129 | + |
| 130 | + expect(resultTable).to.equal(expectedTableString.toString()); |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments