avo-inspector
Version:
[](https://badge.fury.io/js/avo-inspector)
336 lines (335 loc) • 14.1 kB
JavaScript
;
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 AvoInspector_1 = require("../AvoInspector");
var AvoInspectorEnv_1 = require("../AvoInspectorEnv");
var AvoStreamId_1 = require("../AvoStreamId");
var constants_1 = require("../__tests__/constants");
// Mock global fetch to avoid real network calls
var mockFetch = jest.fn();
global.fetch = mockFetch;
describe("Initialization", function () {
test("Api Key is set", function () {
// Given
var apiKey = "api-key-xxx";
// When
var inspector = new AvoInspector_1.AvoInspector({
env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
version: "0",
apiKey: apiKey,
});
// Then
expect(inspector.apiKey).toBe(apiKey);
});
test("Error is thrown when Api Key is not set", function () {
// Given
// @ts-ignore
var apiKey;
// Then
expect(function () {
new AvoInspector_1.AvoInspector({
env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
version: "0",
// @ts-ignore
apiKey: apiKey,
});
}).toThrow(constants_1.error.API_KEY);
});
test("Error is thrown when empty Api Key is used", function () {
// Given
var apiKey = " ";
// Then
expect(function () {
new AvoInspector_1.AvoInspector({
env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
version: "0",
apiKey: apiKey,
});
}).toThrow(constants_1.error.API_KEY);
});
test("Error is thrown when Api Key is set to null", function () {
// Given
var apiKey = null;
// Then
expect(function () {
new AvoInspector_1.AvoInspector({
env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
version: "0",
// @ts-ignore
apiKey: apiKey,
});
}).toThrow(constants_1.error.API_KEY);
});
test("Dev environment is used when env is not provided", function () {
// Given
// @ts-ignore
var env;
// When
var inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
version: "0",
// @ts-ignore
env: env,
});
// Then
expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Dev);
});
test("Dev environment is used when empty string is used", function () {
// Given
var env = "";
// When
var inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
version: "0",
// @ts-ignore
env: env,
});
// Then
expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Dev);
});
test("Dev env is set using AvoInspectorEnv", function () {
// When
var inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: AvoInspectorEnv_1.AvoInspectorEnv.Dev,
version: "0",
});
// Then
expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Dev);
});
test("Dev environment is set using string", function () {
// When
var inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: "dev",
version: "0",
});
// Then
expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Dev);
});
test("Staging env is set using AvoInspectorEnv", function () {
// When
var inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: AvoInspectorEnv_1.AvoInspectorEnv.Staging,
version: "0",
});
// Then
expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Staging);
});
test("Staging environment is set using string", function () {
// When
var inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: "staging",
version: "0",
});
// Then
expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Staging);
});
test("Prod env is set using AvoInspectorEnv", function () {
// When
var inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
version: "0",
});
// Then
expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Prod);
});
test("Prod environment is set using string", function () {
// When
var inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: "prod",
version: "0",
});
// Then
expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Prod);
});
test("Environment other than Dev, Staging, Prod falls back to Dev", function () {
// When
var env = "test";
var inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
version: "0",
// @ts-ignore
env: env,
});
// Then
expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Dev);
});
test("Version is set", function () {
var version = "1";
// When
var inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
version: version,
});
// Then
expect(inspector.version).toBe(version);
});
test("Error is thrown when version is not set", function () {
// Given
// @ts-ignore
var version;
// Then
expect(function () {
new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
// @ts-ignore
version: version,
});
}).toThrow(constants_1.error.VERSION);
});
test("Error is thrown when version is set to empty string", function () {
// Given
var version = " ";
// Then
expect(function () {
new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
version: version,
});
}).toThrow(constants_1.error.VERSION);
});
test("Error is thrown when version is set to null", function () {
// Given
var version = null;
// Then
expect(function () {
new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
// @ts-ignore
version: version,
});
}).toThrow(constants_1.error.VERSION);
});
});
describe("fetchAndValidateEvent integration", function () {
beforeEach(function () {
mockFetch.mockReset();
// Reset AvoStreamId static state between tests
AvoStreamId_1.AvoStreamId._anonymousId = null;
AvoStreamId_1.AvoStreamId._initializationPromise = null;
});
test("dev env creates validator, fetchAndValidateEvent merges validation results into tracked events", function () { return __awaiter(void 0, void 0, void 0, function () {
var wireResponse, inspector, schema, colorProp, calledUrl;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
wireResponse = {
events: [
{
b: "branch1",
id: "evt_1",
vids: [],
p: {
color: {
t: "string",
r: true,
// pinned value: "red" is required for evt_1
p: { red: ["evt_1"] },
},
},
},
],
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];
}); }); },
});
// Inject a known streamId so fetchAndValidateEvent proceeds past the streamId guard
AvoStreamId_1.AvoStreamId._anonymousId = "test-stream-id";
inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: AvoInspectorEnv_1.AvoInspectorEnv.Dev,
version: "1",
});
// Force streamId to be set synchronously (it's fire-and-forget in constructor)
inspector.streamId = "test-stream-id";
return [4 /*yield*/, inspector.trackSchemaFromEvent("Sign Up", {
color: "blue", // does NOT match pinned "red"
})];
case 1:
schema = _a.sent();
colorProp = schema.find(function (p) { return p.propertyName === "color"; });
expect(colorProp).toBeDefined();
// The avoBatcher should have been called with validation data merged in
// We verify this indirectly by checking that fetch was called (validation ran)
expect(mockFetch).toHaveBeenCalledTimes(1);
calledUrl = mockFetch.mock.calls[0][0];
expect(calledUrl).toContain("eventName=Sign+Up");
expect(calledUrl).toContain("apiKey=api-key-xxx");
return [2 /*return*/];
}
});
}); });
test("prod env skips fetchAndValidateEvent (no fetch calls)", function () { return __awaiter(void 0, void 0, void 0, function () {
var inspector;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
inspector = new AvoInspector_1.AvoInspector({
apiKey: "api-key-xxx",
env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
version: "1",
});
// When
return [4 /*yield*/, inspector.trackSchemaFromEvent("Purchase", { amount: 42 })];
case 1:
// When
_a.sent();
// Then: no fetch should have been called in prod
expect(mockFetch).not.toHaveBeenCalled();
return [2 /*return*/];
}
});
}); });
});