|
| 1 | +/** |
| 2 | + * Global setup for integration tests. |
| 3 | + * |
| 4 | + * Creates a schema + tenant before all tests, sets initial values, then |
| 5 | + * tears everything down afterward. The tenantId and serverAddr are |
| 6 | + * provided to tests via Vitest's inject() mechanism. |
| 7 | + */ |
| 8 | + |
| 9 | +import { credentials, Metadata, type ServiceError } from "@grpc/grpc-js"; |
| 10 | +import type { GlobalSetupContext } from "vitest/node"; |
| 11 | +import { ConfigClient } from "../src/client.js"; |
| 12 | +import { ConfigServiceClient } from "../src/generated/centralconfig/v1/config_service.js"; |
| 13 | +import { SchemaServiceClient } from "../src/generated/centralconfig/v1/schema_service.js"; |
| 14 | +import { FieldType } from "../src/generated/centralconfig/v1/types.js"; |
| 15 | + |
| 16 | +declare module "vitest" { |
| 17 | + interface ProvidedContext { |
| 18 | + tenantId: string; |
| 19 | + schemaId: string; |
| 20 | + serverAddr: string; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function promisify<Req, Res>( |
| 25 | + fn: (req: Req, meta: Metadata, cb: (err: ServiceError | null, res: Res) => void) => void, |
| 26 | + req: Req, |
| 27 | + meta: Metadata, |
| 28 | +): Promise<Res> { |
| 29 | + return new Promise((resolve, reject) => { |
| 30 | + fn(req, meta, (err, res) => { |
| 31 | + if (err) reject(err); |
| 32 | + else resolve(res as Res); |
| 33 | + }); |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +export async function setup({ provide }: GlobalSetupContext) { |
| 38 | + const serverAddr = process.env.DECREE_SERVER_ADDR ?? "localhost:9090"; |
| 39 | + const creds = credentials.createInsecure(); |
| 40 | + |
| 41 | + const meta = new Metadata(); |
| 42 | + meta.set("x-subject", "integration-test"); |
| 43 | + meta.set("x-role", "superadmin"); |
| 44 | + |
| 45 | + const schemaClient = new SchemaServiceClient(serverAddr, creds); |
| 46 | + |
| 47 | + // Wait up to 30s for the server to accept gRPC connections. |
| 48 | + await new Promise<void>((resolve, reject) => { |
| 49 | + schemaClient.waitForReady(new Date(Date.now() + 30_000), (err) => { |
| 50 | + if (err) reject(new Error(`server not ready at ${serverAddr}: ${err.message}`)); |
| 51 | + else resolve(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + const schemaName = `ts-sdk-int-${Date.now()}`; |
| 56 | + const { schema } = await promisify( |
| 57 | + schemaClient.createSchema.bind(schemaClient), |
| 58 | + { |
| 59 | + name: schemaName, |
| 60 | + description: "TypeScript SDK integration test schema", |
| 61 | + fields: [ |
| 62 | + { |
| 63 | + path: "app.fee", |
| 64 | + type: FieldType.FIELD_TYPE_STRING, |
| 65 | + constraints: undefined, |
| 66 | + nullable: true, |
| 67 | + deprecated: false, |
| 68 | + examples: {}, |
| 69 | + tags: [], |
| 70 | + }, |
| 71 | + { |
| 72 | + path: "app.count", |
| 73 | + type: FieldType.FIELD_TYPE_INT, |
| 74 | + constraints: undefined, |
| 75 | + nullable: false, |
| 76 | + deprecated: false, |
| 77 | + examples: {}, |
| 78 | + tags: [], |
| 79 | + }, |
| 80 | + { |
| 81 | + path: "app.enabled", |
| 82 | + type: FieldType.FIELD_TYPE_BOOL, |
| 83 | + constraints: undefined, |
| 84 | + nullable: false, |
| 85 | + deprecated: false, |
| 86 | + examples: {}, |
| 87 | + tags: [], |
| 88 | + }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + meta, |
| 92 | + ); |
| 93 | + if (!schema) throw new Error("createSchema returned no schema"); |
| 94 | + const schemaId = schema.id; |
| 95 | + |
| 96 | + await promisify( |
| 97 | + schemaClient.publishSchema.bind(schemaClient), |
| 98 | + { id: schemaId, version: 1 }, |
| 99 | + meta, |
| 100 | + ); |
| 101 | + |
| 102 | + const tenantName = `ts-sdk-tenant-${Date.now()}`; |
| 103 | + const { tenant } = await promisify( |
| 104 | + schemaClient.createTenant.bind(schemaClient), |
| 105 | + { name: tenantName, schemaId, schemaVersion: 1 }, |
| 106 | + meta, |
| 107 | + ); |
| 108 | + if (!tenant) throw new Error("createTenant returned no tenant"); |
| 109 | + const tenantId = tenant.id; |
| 110 | + |
| 111 | + const configClient = new ConfigClient(serverAddr, { |
| 112 | + insecure: true, |
| 113 | + subject: "integration-test", |
| 114 | + role: "superadmin", |
| 115 | + retry: false, |
| 116 | + }); |
| 117 | + const rawConfig = new ConfigServiceClient(serverAddr, creds); |
| 118 | + await configClient.set(tenantId, "app.fee", "0.5%"); |
| 119 | + await promisify( |
| 120 | + rawConfig.setField.bind(rawConfig), |
| 121 | + { tenantId, fieldPath: "app.count", value: { integerValue: 42 } }, |
| 122 | + meta, |
| 123 | + ); |
| 124 | + await configClient.setBool(tenantId, "app.enabled", true); |
| 125 | + configClient.close(); |
| 126 | + rawConfig.close(); |
| 127 | + |
| 128 | + provide("tenantId", tenantId); |
| 129 | + provide("schemaId", schemaId); |
| 130 | + provide("serverAddr", serverAddr); |
| 131 | + |
| 132 | + return async () => { |
| 133 | + await promisify(schemaClient.deleteTenant.bind(schemaClient), { id: tenantId }, meta); |
| 134 | + await promisify(schemaClient.deleteSchema.bind(schemaClient), { id: schemaId }, meta); |
| 135 | + schemaClient.close(); |
| 136 | + }; |
| 137 | +} |
0 commit comments