usage.integration.test.ts52 lines · main
| 1 | /** |
| 2 | * getStorageUsage against REAL DoltGres — sprint plan S2.2. |
| 3 | * |
| 4 | * Proves the storage counter reads the project's OWN database (db-per-project) |
| 5 | * and returns a real size + table count, instead of the old postgres.js + |
| 6 | * schema-per-project path that queried the wrong database and silently |
| 7 | * returned 0. Also confirms `_briven_*` internal tables are excluded. |
| 8 | * |
| 9 | * Skips when BRIVEN_DATA_PLANE_URL is unset. |
| 10 | */ |
| 11 | import { afterAll, beforeAll, describe, expect, test } from 'bun:test'; |
| 12 | |
| 13 | import { |
| 14 | closeProjectDbPools, |
| 15 | dropProjectDatabase, |
| 16 | provisionProjectDatabase, |
| 17 | runInProjectDatabase, |
| 18 | } from '../db/data-plane.js'; |
| 19 | import { getStorageUsage } from './usage.js'; |
| 20 | |
| 21 | const HAS_DB = Boolean(process.env.BRIVEN_DATA_PLANE_URL); |
| 22 | const PROJECT_ID = `p_usage${Date.now().toString(36)}`; |
| 23 | |
| 24 | describe.skipIf(!HAS_DB)('getStorageUsage against real DoltGres (S2.2)', () => { |
| 25 | beforeAll(async () => { |
| 26 | // Provisioning also creates _briven_migrations + _briven_meta, so this |
| 27 | // exercises the internal-table exclusion for free. |
| 28 | await provisionProjectDatabase(PROJECT_ID); |
| 29 | await runInProjectDatabase(PROJECT_ID, async (tx) => { |
| 30 | await tx.unsafe('SET dolt_transaction_commit = 1'); |
| 31 | await tx.unsafe(`CREATE TABLE "items" (id text PRIMARY KEY, payload text)`); |
| 32 | await tx.unsafe(`INSERT INTO "items" (id, payload) VALUES ('1','x'),('2','y'),('3','z')`); |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | afterAll(async () => { |
| 37 | await dropProjectDatabase(PROJECT_ID).catch(() => {}); |
| 38 | await closeProjectDbPools().catch(() => {}); |
| 39 | }); |
| 40 | |
| 41 | test('counts only user tables (not _briven_*) from the correct database', async () => { |
| 42 | const usage = await getStorageUsage(PROJECT_ID); |
| 43 | // The real fix: queries the project's OWN db and counts correctly. Before, |
| 44 | // the wrong-db query returned tableCount 0. |
| 45 | expect(usage.tableCount).toBe(1); // "items" only; _briven_* excluded |
| 46 | // bytes is best-effort: DoltGres reports relation size as 0 today (see the |
| 47 | // KNOWN LIMITATION note in getStorageUsage). Assert it's a non-negative |
| 48 | // number, not that it's > 0. |
| 49 | expect(typeof usage.bytes).toBe('number'); |
| 50 | expect(usage.bytes).toBeGreaterThanOrEqual(0); |
| 51 | }); |
| 52 | }); |