|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as nock from "nock"; |
| 3 | +import * as clc from "colorette"; |
| 4 | + |
| 5 | +import { remoteConfigApiOrigin } from "../api"; |
| 6 | +import { FirebaseError } from "../error"; |
| 7 | +import { deleteExperiment } from "./deleteExperiment"; |
| 8 | +import { NAMESPACE_FIREBASE } from "./interfaces"; |
| 9 | + |
| 10 | +const PROJECT_ID = "12345679"; |
| 11 | +const EXPERIMENT_ID = "1"; |
| 12 | + |
| 13 | +describe("Remote Config Experiment Delete", () => { |
| 14 | + afterEach(() => { |
| 15 | + expect(nock.isDone()).to.equal(true, "all nock stubs should have been called"); |
| 16 | + nock.cleanAll(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should delete an experiment successfully", async () => { |
| 20 | + nock(remoteConfigApiOrigin()) |
| 21 | + .delete( |
| 22 | + `/v1/projects/${PROJECT_ID}/namespaces/${NAMESPACE_FIREBASE}/experiments/${EXPERIMENT_ID}`, |
| 23 | + ) |
| 24 | + .reply(200); |
| 25 | + |
| 26 | + await expect( |
| 27 | + deleteExperiment(PROJECT_ID, NAMESPACE_FIREBASE, EXPERIMENT_ID), |
| 28 | + ).to.eventually.equal(clc.bold(`Successfully deleted experiment ${clc.yellow(EXPERIMENT_ID)}`)); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should throw FirebaseError if experiment is running", async () => { |
| 32 | + const errorMessage = `Experiment ${EXPERIMENT_ID} is currently running and cannot be deleted. If you want to delete this experiment, stop it at https://console.firebase.google.com/project/${PROJECT_ID}/config/experiment/results/${EXPERIMENT_ID}`; |
| 33 | + nock(remoteConfigApiOrigin()) |
| 34 | + .delete( |
| 35 | + `/v1/projects/${PROJECT_ID}/namespaces/${NAMESPACE_FIREBASE}/experiments/${EXPERIMENT_ID}`, |
| 36 | + ) |
| 37 | + .reply(400, { |
| 38 | + error: { |
| 39 | + message: errorMessage, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + await expect( |
| 44 | + deleteExperiment(PROJECT_ID, NAMESPACE_FIREBASE, EXPERIMENT_ID), |
| 45 | + ).to.be.rejectedWith(FirebaseError, errorMessage); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should throw FirebaseError if an internal error occurred", async () => { |
| 49 | + nock(remoteConfigApiOrigin()) |
| 50 | + .delete( |
| 51 | + `/v1/projects/${PROJECT_ID}/namespaces/${NAMESPACE_FIREBASE}/experiments/${EXPERIMENT_ID}`, |
| 52 | + ) |
| 53 | + .reply(500, { |
| 54 | + error: { |
| 55 | + message: "Internal server error", |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + await expect( |
| 60 | + deleteExperiment(PROJECT_ID, NAMESPACE_FIREBASE, EXPERIMENT_ID), |
| 61 | + ).to.be.rejectedWith( |
| 62 | + FirebaseError, |
| 63 | + `Failed to delete Remote Config experiment with ID ${EXPERIMENT_ID} for project ${PROJECT_ID}. Error: Request to https://firebaseremoteconfig.googleapis.com/v1/projects/12345679/namespaces/firebase/experiments/1 had HTTP Error: 500, Internal server error`, |
| 64 | + ); |
| 65 | + }); |
| 66 | +}); |
0 commit comments