137 lines
7.8 KiB
JavaScript
137 lines
7.8 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
// Mock firebase-admin before any module under test is imported.
|
|
// `admin.firestore` must work as both a callable (admin.firestore()) and a
|
|
// namespace (admin.firestore.Timestamp). Object.assign achieves both.
|
|
jest.mock('firebase-admin', () => {
|
|
const firestoreFn = jest.fn();
|
|
firestoreFn.Timestamp = {
|
|
now: jest.fn(() => ({ toMillis: () => 1000000000000 })),
|
|
fromMillis: jest.fn((ms) => ({ toMillis: () => ms })),
|
|
};
|
|
return {
|
|
firestore: firestoreFn,
|
|
apps: [],
|
|
initializeApp: jest.fn(),
|
|
};
|
|
});
|
|
const admin = __importStar(require("firebase-admin"));
|
|
const entitlementLogic_1 = require("./entitlementLogic");
|
|
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
function event(overrides = {}) {
|
|
return Object.assign({ id: 'evt-001', type: 'INITIAL_PURCHASE', app_user_id: 'user-123', product_id: 'closer_premium_monthly', entitlement_id: 'closer_premium' }, overrides);
|
|
}
|
|
function buildFirestoreMock(opts = {}) {
|
|
const mockSet = jest.fn().mockResolvedValue(undefined);
|
|
const mockCreate = jest.fn().mockImplementation(() => opts.createShouldFail
|
|
? Promise.reject(Object.assign(new Error('ALREADY_EXISTS'), { code: 6 }))
|
|
: Promise.resolve(undefined));
|
|
// Leaf doc ref (e.g. users/{uid}/entitlements/premium)
|
|
const deepDocRef = { set: mockSet };
|
|
const deepDocFn = jest.fn().mockReturnValue(deepDocRef);
|
|
const subCollectionRef = { doc: deepDocFn };
|
|
const subCollectionFn = jest.fn().mockReturnValue(subCollectionRef);
|
|
// Top-level doc ref: supports create (idempotency marker) and subcollection access
|
|
const topDocRef = { create: mockCreate, set: mockSet, collection: subCollectionFn };
|
|
const mockDoc = jest.fn().mockReturnValue(topDocRef);
|
|
const mockCollection = jest.fn().mockReturnValue({ doc: mockDoc });
|
|
const mockInstance = { collection: mockCollection };
|
|
admin.firestore.mockReturnValue(mockInstance);
|
|
return { mockSet, mockCreate };
|
|
}
|
|
// ── isPremiumEntitlement ──────────────────────────────────────────────────────
|
|
describe('isPremiumEntitlement', () => {
|
|
it('returns true when entitlement_id matches', () => {
|
|
expect((0, entitlementLogic_1.isPremiumEntitlement)(event({ entitlement_id: 'closer_premium' }))).toBe(true);
|
|
});
|
|
it('returns true when entitlement_ids array includes the id', () => {
|
|
expect((0, entitlementLogic_1.isPremiumEntitlement)(event({ entitlement_id: undefined, entitlement_ids: ['closer_premium', 'other'] }))).toBe(true);
|
|
});
|
|
it('returns false for a different entitlement_id', () => {
|
|
expect((0, entitlementLogic_1.isPremiumEntitlement)(event({ entitlement_id: 'other_entitlement', entitlement_ids: [] }))).toBe(false);
|
|
});
|
|
it('returns false when neither id nor ids reference the premium entitlement', () => {
|
|
expect((0, entitlementLogic_1.isPremiumEntitlement)(event({ entitlement_id: undefined, entitlement_ids: ['not_premium'] }))).toBe(false);
|
|
});
|
|
});
|
|
// ── Event type sets ───────────────────────────────────────────────────────────
|
|
describe('PREMIUM_ACTIVE_TYPES', () => {
|
|
it.each(['INITIAL_PURCHASE', 'RENEWAL', 'PRODUCT_CHANGE', 'TRANSFER', 'UNCANCELLATION'])('contains %s', (type) => expect(entitlementLogic_1.PREMIUM_ACTIVE_TYPES.has(type)).toBe(true));
|
|
it.each(['EXPIRATION', 'CANCELLATION', 'BILLING_ISSUE'])('does not contain revocation event %s', (type) => expect(entitlementLogic_1.PREMIUM_ACTIVE_TYPES.has(type)).toBe(false));
|
|
});
|
|
describe('PREMIUM_REVOKED_TYPES', () => {
|
|
it.each(['EXPIRATION', 'CANCELLATION', 'BILLING_ISSUE', 'SUBSCRIBER_ALIAS'])('contains %s', (type) => expect(entitlementLogic_1.PREMIUM_REVOKED_TYPES.has(type)).toBe(true));
|
|
it.each(['INITIAL_PURCHASE', 'RENEWAL'])('does not contain active event %s', (type) => expect(entitlementLogic_1.PREMIUM_REVOKED_TYPES.has(type)).toBe(false));
|
|
});
|
|
// ── applyEntitlementEvent ─────────────────────────────────────────────────────
|
|
describe('applyEntitlementEvent', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
it('grants premium on INITIAL_PURCHASE', async () => {
|
|
const { mockSet } = buildFirestoreMock();
|
|
await (0, entitlementLogic_1.applyEntitlementEvent)(event({ type: 'INITIAL_PURCHASE' }));
|
|
expect(mockSet).toHaveBeenCalledWith(expect.objectContaining({ premium: true }));
|
|
});
|
|
it('revokes premium on EXPIRATION', async () => {
|
|
const { mockSet } = buildFirestoreMock();
|
|
await (0, entitlementLogic_1.applyEntitlementEvent)(event({ type: 'EXPIRATION' }));
|
|
expect(mockSet).toHaveBeenCalledWith(expect.objectContaining({ premium: false, expiresAt: null }));
|
|
});
|
|
it('revokes premium on CANCELLATION', async () => {
|
|
const { mockSet } = buildFirestoreMock();
|
|
await (0, entitlementLogic_1.applyEntitlementEvent)(event({ type: 'CANCELLATION' }));
|
|
expect(mockSet).toHaveBeenCalledWith(expect.objectContaining({ premium: false }));
|
|
});
|
|
it('is idempotent — skips duplicate event ids', async () => {
|
|
const { mockSet } = buildFirestoreMock({ createShouldFail: true });
|
|
await (0, entitlementLogic_1.applyEntitlementEvent)(event());
|
|
// The idempotency guard fires (create threw ALREADY_EXISTS), so set is never called.
|
|
expect(mockSet).not.toHaveBeenCalled();
|
|
});
|
|
it('stores expiresAt when expiration_at_ms is present', async () => {
|
|
const { mockSet } = buildFirestoreMock();
|
|
const expiresAtMs = Date.now() + 86400000;
|
|
await (0, entitlementLogic_1.applyEntitlementEvent)(event({ type: 'RENEWAL', expiration_at_ms: expiresAtMs }));
|
|
expect(mockSet).toHaveBeenCalledWith(expect.objectContaining({ premium: true }));
|
|
});
|
|
it('ignores non-premium entitlement events', async () => {
|
|
const { mockSet } = buildFirestoreMock();
|
|
await (0, entitlementLogic_1.applyEntitlementEvent)(event({ entitlement_id: 'some_other_entitlement', entitlement_ids: [] }));
|
|
expect(mockSet).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
//# sourceMappingURL=entitlementLogic.test.js.map
|