|
| 1 | +// Health checks for the Google provider plugin. Provider integrations wire the |
| 2 | +// OpenAPI health-check backing and auto-configure a default identity check |
| 3 | +// (People API `people.get`) at add time, so a connection reports alive/expired + |
| 4 | +// identity out of the box. Here we pin the auto-default + the typed candidate the |
| 5 | +// editor would show; the probe itself is the shared OpenAPI path exercised by |
| 6 | +// health-checks.ts. |
| 7 | +import { randomBytes } from "node:crypto"; |
| 8 | +import { createServer } from "node:http"; |
| 9 | + |
| 10 | +import { Effect } from "effect"; |
| 11 | +import { expect } from "@effect/vitest"; |
| 12 | +import type { HttpApiClient } from "effect/unstable/httpapi"; |
| 13 | +import { composePluginApi } from "@executor-js/api/server"; |
| 14 | +import { googleHttpPlugin } from "@executor-js/plugin-google/api"; |
| 15 | +import { IntegrationSlug } from "@executor-js/sdk/shared"; |
| 16 | + |
| 17 | +import { scenario } from "../src/scenario"; |
| 18 | +import { Api, Target } from "../src/services"; |
| 19 | + |
| 20 | +const api = composePluginApi([googleHttpPlugin()] as const); |
| 21 | +type Client = HttpApiClient.ForApi<typeof api>; |
| 22 | + |
| 23 | +const newSlug = (prefix: string) => |
| 24 | + IntegrationSlug.make(`${prefix}-${randomBytes(4).toString("hex")}`); |
| 25 | + |
| 26 | +/** A minimal Google Discovery document for the People API, served locally so |
| 27 | + * `addBundle` (which fetches the discovery URL) is hermetic. `people.get` is the |
| 28 | + * canonical identity call; its response carries `emailAddresses[].value`. */ |
| 29 | +const peopleDiscoveryDoc = (): string => |
| 30 | + JSON.stringify({ |
| 31 | + kind: "discovery#restDescription", |
| 32 | + name: "people", |
| 33 | + version: "v1", |
| 34 | + title: "People API", |
| 35 | + rootUrl: "https://people.example.com/", |
| 36 | + servicePath: "", |
| 37 | + auth: { |
| 38 | + oauth2: { |
| 39 | + scopes: { |
| 40 | + "https://www.googleapis.com/auth/userinfo.email": { description: "See your email" }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + resources: { |
| 45 | + people: { |
| 46 | + methods: { |
| 47 | + get: { |
| 48 | + id: "people.people.get", |
| 49 | + httpMethod: "GET", |
| 50 | + path: "v1/{resourceName}", |
| 51 | + scopes: ["https://www.googleapis.com/auth/userinfo.email"], |
| 52 | + parameters: { |
| 53 | + resourceName: { location: "path", required: true, type: "string" }, |
| 54 | + personFields: { location: "query", type: "string" }, |
| 55 | + }, |
| 56 | + response: { $ref: "Person" }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + schemas: { |
| 62 | + Person: { |
| 63 | + id: "Person", |
| 64 | + type: "object", |
| 65 | + properties: { |
| 66 | + resourceName: { type: "string" }, |
| 67 | + emailAddresses: { type: "array", items: { $ref: "EmailAddress" } }, |
| 68 | + names: { type: "array", items: { $ref: "Name" } }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + EmailAddress: { |
| 72 | + id: "EmailAddress", |
| 73 | + type: "object", |
| 74 | + properties: { value: { type: "string" } }, |
| 75 | + }, |
| 76 | + Name: { id: "Name", type: "object", properties: { displayName: { type: "string" } } }, |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | +/** Serve the People discovery doc at a `/people/`-containing path (so the plugin |
| 81 | + * recognizes the bundle as containing the People API). */ |
| 82 | +const servePeopleDiscovery = () => |
| 83 | + Effect.acquireRelease( |
| 84 | + Effect.callback<{ readonly discoveryUrl: string; readonly close: () => void }>((resume) => { |
| 85 | + const doc = peopleDiscoveryDoc(); |
| 86 | + const server = createServer((request, response) => { |
| 87 | + if ((request.url ?? "").includes("/apis/people/")) { |
| 88 | + response.writeHead(200, { "content-type": "application/json" }); |
| 89 | + response.end(doc); |
| 90 | + return; |
| 91 | + } |
| 92 | + response.writeHead(404, { "content-type": "application/json" }); |
| 93 | + response.end(JSON.stringify({ error: "not_found" })); |
| 94 | + }); |
| 95 | + server.listen(0, "127.0.0.1", () => { |
| 96 | + const address = server.address(); |
| 97 | + const port = typeof address === "object" && address ? address.port : 0; |
| 98 | + resume( |
| 99 | + Effect.succeed({ |
| 100 | + discoveryUrl: `http://127.0.0.1:${port}/discovery/v1/apis/people/v1/rest`, |
| 101 | + close: () => { |
| 102 | + server.close(); |
| 103 | + server.closeAllConnections(); |
| 104 | + }, |
| 105 | + }), |
| 106 | + ); |
| 107 | + }); |
| 108 | + }), |
| 109 | + (server) => Effect.sync(server.close), |
| 110 | + ); |
| 111 | + |
| 112 | +scenario( |
| 113 | + "Health checks · adding a Google People bundle auto-configures the identity check", |
| 114 | + {}, |
| 115 | + Effect.scoped( |
| 116 | + Effect.gen(function* () { |
| 117 | + const target = yield* Target; |
| 118 | + const { client: makeClient } = yield* Api; |
| 119 | + const identity = yield* target.newIdentity(); |
| 120 | + const client: Client = yield* makeClient(api, identity); |
| 121 | + const discovery = yield* servePeopleDiscovery(); |
| 122 | + const slug = newSlug("hc-google"); |
| 123 | + |
| 124 | + yield* Effect.ensuring( |
| 125 | + Effect.gen(function* () { |
| 126 | + yield* client.google.addBundle({ |
| 127 | + payload: { urls: [discovery.discoveryUrl], slug: String(slug) }, |
| 128 | + }); |
| 129 | + |
| 130 | + // The People identity call is offered as a candidate, with its typed |
| 131 | + // response fields (so the editor's identity picker lists the email). |
| 132 | + const candidates = yield* client.integrations.healthCheckCandidates({ params: { slug } }); |
| 133 | + const peopleGet = candidates.find((candidate) => candidate.method === "get"); |
| 134 | + if (!peopleGet) return yield* Effect.die("People bundle exposed no GET candidate"); |
| 135 | + expect( |
| 136 | + (peopleGet.responseFields ?? []).map((field) => field.path), |
| 137 | + "the email identity field is projected from the response schema", |
| 138 | + ).toContain("emailAddresses.0.value"); |
| 139 | + |
| 140 | + // Adding the bundle auto-wrote the default identity health check: the |
| 141 | + // People identity call, with the required pinned args and email field. |
| 142 | + const stored = yield* client.integrations.healthCheckGet({ params: { slug } }); |
| 143 | + expect(stored?.operation, "the default check targets the People identity call").toBe( |
| 144 | + peopleGet.operation, |
| 145 | + ); |
| 146 | + expect(stored?.identityField, "the default reads the email field").toBe( |
| 147 | + "emailAddresses.0.value", |
| 148 | + ); |
| 149 | + expect(stored?.args, "the People call's required args are pinned").toEqual({ |
| 150 | + resourceName: "people/me", |
| 151 | + personFields: "names,emailAddresses", |
| 152 | + }); |
| 153 | + }), |
| 154 | + client.google.removeBundle({ params: { slug: String(slug) } }).pipe(Effect.ignore), |
| 155 | + ); |
| 156 | + }), |
| 157 | + ), |
| 158 | +); |
0 commit comments