From f38091babd3b1cee9d4e1869f066a9bfc05c9206 Mon Sep 17 00:00:00 2001 From: divyanshujethi Date: Tue, 24 Nov 2026 22:59:49 +0530 Subject: [PATCH] fix(ci): format with Biome and eliminate explicit any in billing & store --- src/billing/types.ts | 14 +-- src/metadata/store.ts | 44 +++++++-- src/routes/billing.ts | 35 +++++-- tests/billing.test.ts | 225 ++++++++++++++++++++++-------------------- 4 files changed, 188 insertions(+), 130 deletions(-) diff --git a/src/billing/types.ts b/src/billing/types.ts index c0415fb..e9a99e9 100644 --- a/src/billing/types.ts +++ b/src/billing/types.ts @@ -26,8 +26,8 @@ export const TIER_CONFIGS: Record = { "1 GB remote build cache storage", "Turborepo & Poly CLI protocol support", "7-day cache retention", - "Community GitHub support" - ] + "Community GitHub support", + ], }, pro: { tier: "pro", @@ -44,8 +44,8 @@ export const TIER_CONFIGS: Record = { "30-day cache retention with deduplication", "GitHub Actions OIDC zero-secret authentication", "Sub-15ms cache hits via Cloudflare R2 / S3", - "Priority developer support" - ] + "Priority developer support", + ], }, enterprise: { tier: "enterprise", @@ -62,7 +62,7 @@ export const TIER_CONFIGS: Record = { "90-day retention with LRU watermark auto-eviction", "Cryptographic SLSA Provenance & Ed25519 in-toto verification", "Role-Based Team Tokens & SSO Integration", - "99.99% uptime SLA" - ] - } + "99.99% uptime SLA", + ], + }, }; diff --git a/src/metadata/store.ts b/src/metadata/store.ts index ab2b824..f811ed4 100644 --- a/src/metadata/store.ts +++ b/src/metadata/store.ts @@ -68,7 +68,18 @@ export class MemoryMetadataStore implements MetadataStore { async updateWorkspace( id: string, - updates: Partial> + updates: Partial< + Pick< + Workspace, + | "name" + | "quotaBytes" + | "tier" + | "subscriptionId" + | "subscriptionStatus" + | "subscriptionExpiresAt" + | "defaultTtlSeconds" + > + >, ): Promise { const ws = this.workspaces.get(id); if (!ws) return null; @@ -549,6 +560,10 @@ export class SqliteMetadataStore extends MemoryMetadataStore { slug: string; name: string; quota_bytes: number; + tier?: WorkspaceTier; + subscription_id?: string; + subscription_status?: string; + subscription_expires_at?: string; default_ttl_seconds: number; created_at: string; }>; @@ -558,10 +573,10 @@ export class SqliteMetadataStore extends MemoryMetadataStore { slug: r.slug, name: r.name, quotaBytes: r.quota_bytes, - tier: (r as any).tier || "free", - subscriptionId: (r as any).subscription_id, - subscriptionStatus: (r as any).subscription_status, - subscriptionExpiresAt: (r as any).subscription_expires_at, + tier: r.tier || "free", + subscriptionId: r.subscription_id, + subscriptionStatus: r.subscription_status, + subscriptionExpiresAt: r.subscription_expires_at, defaultTtlSeconds: r.default_ttl_seconds, createdAt: r.created_at, }; @@ -599,12 +614,25 @@ export class SqliteMetadataStore extends MemoryMetadataStore { override async updateWorkspace( id: string, - updates: Partial> + updates: Partial< + Pick< + Workspace, + | "name" + | "quotaBytes" + | "tier" + | "subscriptionId" + | "subscriptionStatus" + | "subscriptionExpiresAt" + | "defaultTtlSeconds" + > + >, ): Promise { const updated = await super.updateWorkspace(id, updates); if (!updated || !this.db) return updated; try { - const db = this.db as any; + const db = this.db as { + prepare: (sql: string) => { run: (...args: unknown[]) => void }; + }; db.prepare(` UPDATE workspaces SET name = COALESCE(?, name), @@ -623,7 +651,7 @@ export class SqliteMetadataStore extends MemoryMetadataStore { updates.subscriptionStatus ?? null, updates.subscriptionExpiresAt ?? null, updates.defaultTtlSeconds ?? null, - id + id, ); } catch {} return updated; diff --git a/src/routes/billing.ts b/src/routes/billing.ts index c7ecd0e..961265e 100644 --- a/src/routes/billing.ts +++ b/src/routes/billing.ts @@ -1,8 +1,8 @@ -import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; import * as crypto from "node:crypto"; +import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; +import { TIER_CONFIGS } from "../billing/types.js"; import type { PolyCloudConfig } from "../config.js"; import type { MetadataStore, WorkspaceTier } from "../metadata/types.js"; -import { TIER_CONFIGS } from "../billing/types.js"; export interface BillingRouteOptions { store: MetadataStore; @@ -111,7 +111,8 @@ export async function registerBillingRoutes( subscriptionId: ws.subscriptionId, subscriptionStatus: ws.subscriptionStatus || "none", subscriptionExpiresAt: ws.subscriptionExpiresAt, - upgradeAvailable: tier === "free" ? ["pro", "enterprise"] : tier === "pro" ? ["enterprise"] : [], + upgradeAvailable: + tier === "free" ? ["pro", "enterprise"] : tier === "pro" ? ["enterprise"] : [], }); }, ); @@ -124,7 +125,7 @@ export async function registerBillingRoutes( const sigHeader = (request.headers["webhook-signature"] as string) || (request.headers["x-dodo-signature"] as string) || - (request.headers["signature"] as string); + (request.headers.signature as string); if (!webhookSecret) { return reply.code(500).send({ @@ -166,14 +167,34 @@ export async function registerBillingRoutes( message: "Invalid Dodo Payments signature", }); } - } catch (err: any) { + } catch (err: unknown) { + const msg = err instanceof Error ? err.message : String(err); return reply.code(401).send({ error: "Unauthorized", - message: `Signature verification failed: ${err.message}`, + message: `Signature verification failed: ${msg}`, }); } - const payload = (typeof request.body === "object" ? request.body : JSON.parse(rawBody)) as any; + interface DodoWebhookPayload { + id?: string; + event_id?: string; + type?: string; + event_type?: string; + data?: { + payment_id?: string; + subscription_id?: string; + product_id?: string; + next_billing_date?: string; + metadata?: Record; + customer?: { + metadata?: Record; + }; + }; + } + + const payload = ( + typeof request.body === "object" ? request.body : JSON.parse(rawBody) + ) as DodoWebhookPayload; const eventType = payload.type || payload.event_type; const data = payload.data || {}; diff --git a/tests/billing.test.ts b/tests/billing.test.ts index 320e8d7..98c339a 100644 --- a/tests/billing.test.ts +++ b/tests/billing.test.ts @@ -65,87 +65,93 @@ test("Poly Remote Cloud Cache Tiering & Billing Suite", async (t) => { assert.ok(body.checkoutUrl.includes("email=lead%40startup.io")); }); - await t.test("PUT /v8/artifacts/:hash returns 402 Payment Required when quota is exceeded", async () => { - // Artifact size is 600 bytes, which exceeds the 500-byte quota - const artifactPayload = Buffer.alloc(600, "x"); - const res = await app.inject({ - method: "PUT", - url: "/v8/artifacts/oversized-hash?team=startup-team", - headers: { - authorization: `Bearer ${writeToken}`, - "content-type": "application/octet-stream", - "content-length": "600", - }, - payload: artifactPayload, - }); - - assert.equal(res.statusCode, 402); - assert.ok(res.headers["x-poly-upgrade-url"]); - const body = JSON.parse(res.body); - assert.equal(body.error, "Payment Required"); - assert.equal(body.code, "QUOTA_EXCEEDED"); - assert.equal(body.tier, "free"); - assert.ok(body.upgradeUrl.includes("checkout.dodopayments.com")); - }); - - await t.test("POST /api/v1/billing/webhooks/dodo enforces signature and upgrades workspace to Pro", async () => { - process.env.DODO_WEBHOOK_SECRET = webhookSecret; - - const payload = { - event_id: "evt_sub_active_101", - type: "subscription.active", - data: { - subscription_id: "sub_dodo_live_999", - product_id: "pdt_0NpjONNytUpW7gzjoDlYW", - metadata: { - workspace: "startup-team", - tier: "pro", + await t.test( + "PUT /v8/artifacts/:hash returns 402 Payment Required when quota is exceeded", + async () => { + // Artifact size is 600 bytes, which exceeds the 500-byte quota + const artifactPayload = Buffer.alloc(600, "x"); + const res = await app.inject({ + method: "PUT", + url: "/v8/artifacts/oversized-hash?team=startup-team", + headers: { + authorization: `Bearer ${writeToken}`, + "content-type": "application/octet-stream", + "content-length": "600", }, - }, - }; - - const rawBody = JSON.stringify(payload); - const sig = crypto.createHmac("sha256", webhookSecret).update(rawBody).digest("hex"); + payload: artifactPayload, + }); + + assert.equal(res.statusCode, 402); + assert.ok(res.headers["x-poly-upgrade-url"]); + const body = JSON.parse(res.body); + assert.equal(body.error, "Payment Required"); + assert.equal(body.code, "QUOTA_EXCEEDED"); + assert.equal(body.tier, "free"); + assert.ok(body.upgradeUrl.includes("checkout.dodopayments.com")); + }, + ); + + await t.test( + "POST /api/v1/billing/webhooks/dodo enforces signature and upgrades workspace to Pro", + async () => { + process.env.DODO_WEBHOOK_SECRET = webhookSecret; + + const payload = { + event_id: "evt_sub_active_101", + type: "subscription.active", + data: { + subscription_id: "sub_dodo_live_999", + product_id: "pdt_0NpjONNytUpW7gzjoDlYW", + metadata: { + workspace: "startup-team", + tier: "pro", + }, + }, + }; - const res = await app.inject({ - method: "POST", - url: "/api/v1/billing/webhooks/dodo", - headers: { - "webhook-signature": `sha256=${sig}`, - "content-type": "application/json", - }, - payload, - }); + const rawBody = JSON.stringify(payload); + const sig = crypto.createHmac("sha256", webhookSecret).update(rawBody).digest("hex"); - assert.equal(res.statusCode, 200); - const body = JSON.parse(res.body); - assert.equal(body.success, true); - assert.equal(body.tier, "pro"); - assert.equal(body.quotaBytes, 50 * 1024 * 1024 * 1024); - - // Verify workspace in store is now Pro - const ws = await store.getWorkspaceBySlug("startup-team"); - assert.equal(ws?.tier, "pro"); - assert.equal(ws?.quotaBytes, 50 * 1024 * 1024 * 1024); - assert.equal(ws?.subscriptionStatus, "active"); - - // Upload the 600-byte artifact again: now it must succeed on the 50 GB Pro tier! - const artifactPayload = Buffer.alloc(600, "x"); - const uploadRes = await app.inject({ - method: "PUT", - url: "/v8/artifacts/oversized-hash?team=startup-team", - headers: { - authorization: `Bearer ${writeToken}`, - "content-type": "application/octet-stream", - "content-length": "600", - }, - payload: artifactPayload, - }); + const res = await app.inject({ + method: "POST", + url: "/api/v1/billing/webhooks/dodo", + headers: { + "webhook-signature": `sha256=${sig}`, + "content-type": "application/json", + }, + payload, + }); + + assert.equal(res.statusCode, 200); + const body = JSON.parse(res.body); + assert.equal(body.success, true); + assert.equal(body.tier, "pro"); + assert.equal(body.quotaBytes, 50 * 1024 * 1024 * 1024); + + // Verify workspace in store is now Pro + const ws = await store.getWorkspaceBySlug("startup-team"); + assert.equal(ws?.tier, "pro"); + assert.equal(ws?.quotaBytes, 50 * 1024 * 1024 * 1024); + assert.equal(ws?.subscriptionStatus, "active"); + + // Upload the 600-byte artifact again: now it must succeed on the 50 GB Pro tier! + const artifactPayload = Buffer.alloc(600, "x"); + const uploadRes = await app.inject({ + method: "PUT", + url: "/v8/artifacts/oversized-hash?team=startup-team", + headers: { + authorization: `Bearer ${writeToken}`, + "content-type": "application/octet-stream", + "content-length": "600", + }, + payload: artifactPayload, + }); - assert.equal(uploadRes.statusCode, 200); - const uploadBody = JSON.parse(uploadRes.body); - assert.equal(uploadBody.status, "stored"); - }); + assert.equal(uploadRes.statusCode, 200); + const uploadBody = JSON.parse(uploadRes.body); + assert.equal(uploadBody.status, "stored"); + }, + ); await t.test("GET /api/v1/workspaces/:slug/billing reports active Pro status", async () => { const res = await app.inject({ @@ -161,38 +167,41 @@ test("Poly Remote Cloud Cache Tiering & Billing Suite", async (t) => { assert.ok(body.usedBytes >= 600); }); - await t.test("POST /api/v1/billing/webhooks/dodo downgrades workspace on subscription.cancelled", async () => { - const payload = { - event_id: "evt_sub_cancel_102", - type: "subscription.cancelled", - data: { - subscription_id: "sub_dodo_live_999", - metadata: { - workspace: "startup-team", + await t.test( + "POST /api/v1/billing/webhooks/dodo downgrades workspace on subscription.cancelled", + async () => { + const payload = { + event_id: "evt_sub_cancel_102", + type: "subscription.cancelled", + data: { + subscription_id: "sub_dodo_live_999", + metadata: { + workspace: "startup-team", + }, }, - }, - }; + }; - const rawBody = JSON.stringify(payload); - const sig = crypto.createHmac("sha256", webhookSecret).update(rawBody).digest("hex"); + const rawBody = JSON.stringify(payload); + const sig = crypto.createHmac("sha256", webhookSecret).update(rawBody).digest("hex"); - const res = await app.inject({ - method: "POST", - url: "/api/v1/billing/webhooks/dodo", - headers: { - "webhook-signature": `sha256=${sig}`, - "content-type": "application/json", - }, - payload, - }); - - assert.equal(res.statusCode, 200); - const body = JSON.parse(res.body); - assert.equal(body.tier, "free"); - assert.equal(body.quotaBytes, 1024 * 1024 * 1024); - - const ws = await store.getWorkspaceBySlug("startup-team"); - assert.equal(ws?.tier, "free"); - assert.equal(ws?.subscriptionStatus, "cancelled"); - }); + const res = await app.inject({ + method: "POST", + url: "/api/v1/billing/webhooks/dodo", + headers: { + "webhook-signature": `sha256=${sig}`, + "content-type": "application/json", + }, + payload, + }); + + assert.equal(res.statusCode, 200); + const body = JSON.parse(res.body); + assert.equal(body.tier, "free"); + assert.equal(body.quotaBytes, 1024 * 1024 * 1024); + + const ws = await store.getWorkspaceBySlug("startup-team"); + assert.equal(ws?.tier, "free"); + assert.equal(ws?.subscriptionStatus, "cancelled"); + }, + ); });