|
| 1 | +import { test, expect, describe, beforeAll, afterAll } from "bun:test"; |
| 2 | +import pg from "pg"; |
| 3 | +import { |
| 4 | + getCalendarUsers, |
| 5 | + getAuthToken, |
| 6 | + getAuthTokens, |
| 7 | + getGroups, |
| 8 | + getRoutes, |
| 9 | + getAllRoutes, |
| 10 | + resolveRouteFromDb, |
| 11 | +} from "./db"; |
| 12 | + |
| 13 | +// These tests require POSTGRESQL_ADDON_URI to be set and the DB to be migrated+seeded. |
| 14 | +// They read existing data — no writes, no cleanup needed. |
| 15 | + |
| 16 | +const hasDb = !!process.env.POSTGRESQL_ADDON_URI; |
| 17 | + |
| 18 | +describe.skipIf(!hasDb)("db queries", () => { |
| 19 | + test("getAuthTokens returns entries with masked tokens", async () => { |
| 20 | + const tokens = await getAuthTokens(); |
| 21 | + expect(tokens.length).toBeGreaterThan(0); |
| 22 | + for (const t of tokens) { |
| 23 | + expect(t.name).toBeTruthy(); |
| 24 | + expect(t.tokenPreview).toMatch(/^.{8}\.\.\..{4}$/); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + test("getAuthToken returns token for known name", async () => { |
| 29 | + const token = await getAuthToken("skipper"); |
| 30 | + expect(token).toBeTruthy(); |
| 31 | + expect(typeof token).toBe("string"); |
| 32 | + }); |
| 33 | + |
| 34 | + test("getAuthToken returns null for unknown name", async () => { |
| 35 | + const token = await getAuthToken("nonexistent_token_xyz"); |
| 36 | + expect(token).toBeNull(); |
| 37 | + }); |
| 38 | + |
| 39 | + test("getGroups returns entries with required fields", async () => { |
| 40 | + const groups = await getGroups(); |
| 41 | + expect(groups.length).toBeGreaterThan(0); |
| 42 | + for (const g of groups) { |
| 43 | + expect(g.name).toBeTruthy(); |
| 44 | + expect(g.slashworkId).toBeTruthy(); |
| 45 | + expect(g.authToken).toBeTruthy(); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + test("getRoutes returns entries with correct shape", async () => { |
| 50 | + const routes = await getRoutes(); |
| 51 | + expect(routes.length).toBeGreaterThan(0); |
| 52 | + for (const r of routes) { |
| 53 | + expect(r.name).toBeTruthy(); |
| 54 | + // Each route is either group-based or stream-based |
| 55 | + if (r.groupName) { |
| 56 | + expect(r.streamId).toBeNull(); |
| 57 | + expect(r.authToken).toBeNull(); |
| 58 | + } else { |
| 59 | + expect(r.streamId).toBeTruthy(); |
| 60 | + expect(r.authToken).toBeTruthy(); |
| 61 | + } |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + test("getAllRoutes resolves target IDs and auth tokens", async () => { |
| 66 | + const routes = await getAllRoutes(); |
| 67 | + expect(routes.length).toBeGreaterThan(0); |
| 68 | + for (const r of routes) { |
| 69 | + expect(r.name).toBeTruthy(); |
| 70 | + expect(r.targetId).toBeTruthy(); |
| 71 | + expect(r.authToken).toBeTruthy(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + test("resolveRouteFromDb returns resolved route for known name", async () => { |
| 76 | + const route = await resolveRouteFromDb("skipper"); |
| 77 | + expect(route).not.toBeNull(); |
| 78 | + expect(route!.targetId).toBeTruthy(); |
| 79 | + expect(route!.authToken).toBeTruthy(); |
| 80 | + }); |
| 81 | + |
| 82 | + test("resolveRouteFromDb returns null for unknown route", async () => { |
| 83 | + const route = await resolveRouteFromDb("nonexistent_route_xyz"); |
| 84 | + expect(route).toBeNull(); |
| 85 | + }); |
| 86 | + |
| 87 | + test("getCalendarUsers returns entries with required fields", async () => { |
| 88 | + const users = await getCalendarUsers(); |
| 89 | + expect(users.length).toBeGreaterThan(0); |
| 90 | + for (const u of users) { |
| 91 | + expect(u.name).toBeTruthy(); |
| 92 | + expect(u.calendarId).toBeTruthy(); |
| 93 | + expect(u.targetId).toBeTruthy(); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + test("getAuthToken returns calendar token", async () => { |
| 98 | + const token = await getAuthToken("google_calendar"); |
| 99 | + expect(token).toBeTruthy(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments