UNPKG

avo-inspector

Version:

[![npm version](https://badge.fury.io/js/avo-inspector.svg)](https://badge.fury.io/js/avo-inspector)

378 lines (377 loc) 18.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var AvoEventSpecFetcher_1 = require("../eventSpec/AvoEventSpecFetcher"); // Mock global fetch var mockFetch = jest.fn(); global.fetch = mockFetch; function makeValidWireResponse() { return { events: [ { b: "branch1", id: "evt_1", vids: ["var_1"], p: { method: { t: "string", r: true, p: { email: ["evt_1"] } }, }, }, ], metadata: { schemaId: "schema1", branchId: "branch1", latestActionId: "action1", }, }; } describe("AvoEventSpecFetcher", function () { var fetcher; beforeEach(function () { mockFetch.mockReset(); fetcher = new AvoEventSpecFetcher_1.AvoEventSpecFetcher(2000, false, "dev", "https://api.test.com"); }); test("returns null in production environment", function () { return __awaiter(void 0, void 0, void 0, function () { var prodFetcher, result; return __generator(this, function (_a) { switch (_a.label) { case 0: prodFetcher = new AvoEventSpecFetcher_1.AvoEventSpecFetcher(2000, false, "prod"); return [4 /*yield*/, prodFetcher.fetch({ apiKey: "key", streamId: "stream", eventName: "test", })]; case 1: result = _a.sent(); expect(result).toBeNull(); expect(mockFetch).not.toHaveBeenCalled(); return [2 /*return*/]; } }); }); }); test("fetches and parses wire format correctly", function () { return __awaiter(void 0, void 0, void 0, function () { var wireResponse, result; return __generator(this, function (_a) { switch (_a.label) { case 0: wireResponse = makeValidWireResponse(); mockFetch.mockResolvedValue({ status: 200, json: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, wireResponse]; }); }); }, }); return [4 /*yield*/, fetcher.fetch({ apiKey: "key", streamId: "stream", eventName: "test", })]; case 1: result = _a.sent(); expect(result).not.toBeNull(); expect(result.events[0].branchId).toBe("branch1"); expect(result.events[0].baseEventId).toBe("evt_1"); expect(result.events[0].variantIds).toEqual(["var_1"]); expect(result.events[0].props.method.type).toBe("string"); expect(result.events[0].props.method.required).toBe(true); expect(result.events[0].props.method.pinnedValues).toEqual({ email: ["evt_1"], }); expect(result.metadata.schemaId).toBe("schema1"); return [2 /*return*/]; } }); }); }); test("builds correct URL with query parameters", function () { return __awaiter(void 0, void 0, void 0, function () { var url; return __generator(this, function (_a) { switch (_a.label) { case 0: mockFetch.mockResolvedValue({ status: 200, json: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, makeValidWireResponse()]; }); }); }, }); return [4 /*yield*/, fetcher.fetch({ apiKey: "my-key", streamId: "my-stream", eventName: "Sign Up", })]; case 1: _a.sent(); expect(mockFetch).toHaveBeenCalledWith(expect.stringContaining("https://api.test.com/trackingPlan/eventSpec?"), expect.objectContaining({ method: "GET" })); url = mockFetch.mock.calls[0][0]; expect(url).toContain("apiKey=my-key"); expect(url).toContain("streamId=my-stream"); expect(url).toContain("eventName=Sign+Up"); return [2 /*return*/]; } }); }); }); test("returns null on non-200 status", function () { return __awaiter(void 0, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: mockFetch.mockResolvedValue({ status: 404, json: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, ({})]; }); }); }, }); return [4 /*yield*/, fetcher.fetch({ apiKey: "key", streamId: "stream", eventName: "test", })]; case 1: result = _a.sent(); expect(result).toBeNull(); return [2 /*return*/]; } }); }); }); test("returns null on invalid response shape", function () { return __awaiter(void 0, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: mockFetch.mockResolvedValue({ status: 200, json: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, ({ invalid: true })]; }); }); }, }); return [4 /*yield*/, fetcher.fetch({ apiKey: "key", streamId: "stream", eventName: "test", })]; case 1: result = _a.sent(); expect(result).toBeNull(); return [2 /*return*/]; } }); }); }); test("returns null on network error", function () { return __awaiter(void 0, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: mockFetch.mockRejectedValue(new Error("Network error")); return [4 /*yield*/, fetcher.fetch({ apiKey: "key", streamId: "stream", eventName: "test", })]; case 1: result = _a.sent(); expect(result).toBeNull(); return [2 /*return*/]; } }); }); }); test("returns null on JSON parse error", function () { return __awaiter(void 0, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: mockFetch.mockResolvedValue({ status: 200, json: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { throw new Error("Invalid JSON"); }); }); }, }); return [4 /*yield*/, fetcher.fetch({ apiKey: "key", streamId: "stream", eventName: "test", })]; case 1: result = _a.sent(); expect(result).toBeNull(); return [2 /*return*/]; } }); }); }); test("in-flight de-duplication: exactly ONE fetch for concurrent same-key events", function () { return __awaiter(void 0, void 0, void 0, function () { var resolveJson, jsonPromise, params, promise1, promise2, _a, result1, result2; return __generator(this, function (_b) { switch (_b.label) { case 0: jsonPromise = new Promise(function (resolve) { resolveJson = resolve; }); mockFetch.mockReturnValue(Promise.resolve({ status: 200, json: function () { return jsonPromise; }, })); params = { apiKey: "key", streamId: "stream", eventName: "test" }; promise1 = fetcher.fetch(params); promise2 = fetcher.fetch(params); // Resolve the JSON resolveJson(makeValidWireResponse()); return [4 /*yield*/, Promise.all([promise1, promise2])]; case 1: _a = _b.sent(), result1 = _a[0], result2 = _a[1]; // Both should get the same result expect(result1).toEqual(result2); // Only one fetch call should have been made expect(mockFetch).toHaveBeenCalledTimes(1); return [2 /*return*/]; } }); }); }); test("in-flight failure: all queued callbacks resolved with null on fetch failure", function () { return __awaiter(void 0, void 0, void 0, function () { var rejectFetch, fetchPromise, params, promise1, promise2, _a, result1, result2; return __generator(this, function (_b) { switch (_b.label) { case 0: fetchPromise = new Promise(function (_resolve, reject) { rejectFetch = reject; }); mockFetch.mockReturnValue(fetchPromise); params = { apiKey: "key", streamId: "stream", eventName: "test" }; promise1 = fetcher.fetch(params); promise2 = fetcher.fetch(params); rejectFetch(new Error("Network failure")); return [4 /*yield*/, Promise.all([promise1, promise2])]; case 1: _a = _b.sent(), result1 = _a[0], result2 = _a[1]; expect(result1).toBeNull(); expect(result2).toBeNull(); return [2 /*return*/]; } }); }); }); test("works in staging environment", function () { return __awaiter(void 0, void 0, void 0, function () { var stagingFetcher, result; return __generator(this, function (_a) { switch (_a.label) { case 0: stagingFetcher = new AvoEventSpecFetcher_1.AvoEventSpecFetcher(2000, false, "staging", "https://api.test.com"); mockFetch.mockResolvedValue({ status: 200, json: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, makeValidWireResponse()]; }); }); }, }); return [4 /*yield*/, stagingFetcher.fetch({ apiKey: "key", streamId: "stream", eventName: "test", })]; case 1: result = _a.sent(); expect(result).not.toBeNull(); return [2 /*return*/]; } }); }); }); test("parses property constraints including children, lists, regex, allowedValues, minmax", function () { return __awaiter(void 0, void 0, void 0, function () { var wireResponse, result, props; return __generator(this, function (_a) { switch (_a.label) { case 0: wireResponse = { events: [ { b: "branch1", id: "evt_1", vids: [], p: { name: { t: "string", r: true, rx: { "^[A-Z]": ["evt_1"] }, v: { '["option1","option2"]': ["evt_1"] }, }, age: { t: "int", r: false, minmax: { "0,120": ["evt_1"] }, }, items: { t: "object", r: false, l: true, children: { id: { t: "string", r: true }, }, }, }, }, ], metadata: { schemaId: "schema1", branchId: "branch1", latestActionId: "action1", }, }; mockFetch.mockResolvedValue({ status: 200, json: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, wireResponse]; }); }); }, }); return [4 /*yield*/, fetcher.fetch({ apiKey: "key", streamId: "stream", eventName: "test", })]; case 1: result = _a.sent(); expect(result).not.toBeNull(); props = result.events[0].props; expect(props.name.regexPatterns).toEqual({ "^[A-Z]": ["evt_1"] }); expect(props.name.allowedValues).toEqual({ '["option1","option2"]': ["evt_1"], }); expect(props.age.minMaxRanges).toEqual({ "0,120": ["evt_1"] }); expect(props.items.isList).toBe(true); expect(props.items.children).toBeDefined(); expect(props.items.children.id.type).toBe("string"); return [2 /*return*/]; } }); }); }); });