siegebreak/services/test/products.test.ts
caelum0x 540fa522a0 feat: real IAP receipt verification (Apple + Google + dev provider)
Replaces the throw-stub with full server-side verification:
- Apple: POST verifyReceipt with prod->sandbox (21007) fallback, grants
  from the newest verified in_app product.
- Google Play: purchases.products.get via the Play Developer API, with a
  service-account OAuth token minted from GOOGLE_SERVICE_ACCOUNT_JSON
  using jose (JWT-bearer grant) in economy/googleAuth.ts.
- IAP_MODE=dev provider accepts dev:<productId> so the full
  purchase->grant flow runs locally with no store credentials.

Gems come from a server-authoritative catalog (economy/products.ts)
keyed by the verified productId, never a client field. New
GET /economy/products exposes it. Wallet grant switched update->upsert
so a first-purchase player still gets gems.

19 new tests (catalog, verification for all three paths, economy route
e2e in dev mode). 98 backend tests green.
2026-07-27 17:41:05 +03:00

32 lines
1.1 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { gemsForProduct, listProducts } from "../src/modules/economy/products";
describe("gemsForProduct", () => {
it("returns the configured gems for a known product", () => {
expect(gemsForProduct("gems_small")).toBe(100);
expect(gemsForProduct("gems_mega")).toBe(2500);
});
it("returns null for an unknown product rather than a zero grant", () => {
expect(gemsForProduct("gems_free_lol")).toBeNull();
});
it("does not treat inherited object props as products", () => {
// Guards against a prototype-pollution style lookup ("toString", etc.).
expect(gemsForProduct("toString")).toBeNull();
expect(gemsForProduct("constructor")).toBeNull();
});
});
describe("listProducts", () => {
it("returns the full catalog as {productId, gems} entries", () => {
const products = listProducts();
expect(products.length).toBeGreaterThan(0);
for (const p of products) {
expect(typeof p.productId).toBe("string");
expect(typeof p.gems).toBe("number");
}
expect(products).toContainEqual({ productId: "gems_small", gems: 100 });
});
});