Engineering Standards & Testing
To maintain production stability across all 35 Edge Functions, engineers must adhere to the following coding conventions and testing practices.
1. Coding & TypeScript Conventions
Explicit Imports
Always import packages from verified Deno registries or ESM CDNs with pinned version numbers:
// Standard library (pinned release)import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
// Supabase client (pinned release via esm.sh)import { createClient } from "https://esm.sh/@supabase/supabase-js@2.39.0";Standard Handler Pattern
Every Edge Function must implement the standard CORS preflight check and wrapped error handling:
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
export const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',};
serve(async (req: Request) => { // 1. Handle preflight OPTIONS if (req.method === 'OPTIONS') { return new Response('ok', { headers: corsHeaders }); }
try { // 2. Validate input and authenticate const payload = await req.json();
// 3. Execute domain logic const result = await processDomainLogic(payload);
// 4. Return success response return new Response(JSON.stringify({ success: true, data: result }), { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' }, }); } catch (error) { console.error(`[FUNCTION_ERROR]:`, error);
// 5. Return sanitized error return new Response(JSON.stringify({ success: false, error: error instanceof Error ? error.message : 'Unknown server error', }), { status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' }, }); }});2. Formatting & Linting
We enforce Deno’s native formatting and linter tools. Run before opening pull requests:
# Check formattingdeno fmt --check
# Apply formattingdeno fmt
# Run static analysis / linterdeno lint3. Writing Unit Tests
Tests are authored using Deno.test and standard assertions from std/testing/asserts.ts:
Example: Testing Calculation Logic (billing-calculator_test.ts)
import { assertEquals } from "https://deno.land/std@0.168.0/testing/asserts.ts";import { calculateMemberBalance } from "./calculator_utils.ts";
Deno.test("Calculates net bill with 100% solar coverage correctly", () => { const result = calculateMemberBalance({ consumedKwh: 150, generatedShareKwh: 150, tariffRatePln: 0.85, vatRate: 0.23, });
// Since generation covers consumption, net energy cost is 0 PLN (only fixed fees) assertEquals(result.netEnergyCostPln, 0.0); assertEquals(result.grossTotalPln > 0, true);});Running Tests Locally
deno test --allow-env --allow-net