import assert from "node:assert/strict";
import test from "node:test";
import { createProBillingAdapter } from "./pro-billing.js";

test("web billing is unavailable and native output drops purchase tokens and extra fields", async () => {
  assert.deepEqual(await createProBillingAdapter({ isNative: false }).getProduct(), { productId: "pro_multi_account", localizedPrice: null, available: false });
  const adapter = createProBillingAdapter({ isNative: true, plugin: {
    async purchase() { return { state: "activated", certificate: "signed", expiresAt: "2026-09-01T00:00:00Z", purchaseToken: "secret", extra: true }; }
  }});
  assert.deepEqual(await adapter.purchase({ backendKeyThumbprint: "a".repeat(43), challengeHash: "b".repeat(43) }), {
    state: "activated", certificate: "signed", expiresAt: "2026-09-01T00:00:00Z"
  });
});

test("malformed native responses fail closed", async () => {
  const adapter = createProBillingAdapter({ isNative: true, plugin: { async restore() { return { state: "activated", purchaseToken: "secret" }; } } });
  assert.deepEqual(await adapter.restore({ backendKeyThumbprint: "a".repeat(43), challengeHash: "b".repeat(43) }), { state: "unavailable" });
});

test("purchase management opens the fixed Google Play order history", async () => {
  const opened = [];
  const adapter = createProBillingAdapter({ isNative: false, browser: { async open(options) { opened.push(options); } } });
  assert.deepEqual(await adapter.openManagement(), { opened: true });
  assert.deepEqual(opened, [{ url: "https://play.google.com/store/account/orderhistory" }]);
});
