The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Function } from "@alchemy.run/neon/Function"; |
| 2 | import { FunctionLogs } from "@alchemy.run/neon/FunctionProvider"; |
| 3 | import { providers } from "@alchemy.run/neon/Providers"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as SDK from "@distilled.cloud/neon"; |
| 7 | import * as Alchemy from "alchemy"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 12 | import * as Redacted from "effect/Redacted"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | import Constructor from "./fixtures/function-constructor.ts"; |
| 15 | import { project } from "./fixtures/function-form-resources.ts"; |
| 16 | import LayerLive, { LayerFunction } from "./fixtures/function-layer.ts"; |
| 17 | |
| 18 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 19 | providers: providers(), |
| 20 | }); |
| 21 | const Stack = Alchemy.Stack( |
| 22 | "NeonFunctionForms", |
| 23 | { providers: providers(), state: Alchemy.localState() }, |
| 24 | Effect.gen(function* () { |
| 25 | const scope = yield* project; |
| 26 | const constructor = yield* Constructor; |
| 27 | const layer = yield* LayerFunction; |
| 28 | const hono = yield* Function("Hono", { |
| 29 | project: scope, |
| 30 | main: new URL("./fixtures/function-hono.ts", import.meta.url).href, |
| 31 | }); |
| 32 | // `project` takes the project resource's Effect directly. |
| 33 | const bare = yield* Function("Bare", { |
| 34 | project, |
| 35 | main: new URL("./fixtures/function-bare.ts", import.meta.url).href, |
| 36 | }); |
| 37 | return { |
| 38 | constructor: constructor.url, |
| 39 | layer: layer.url, |
| 40 | hono: hono.url, |
| 41 | bare: bare.url, |
| 42 | host: hono, |
| 43 | }; |
| 44 | }).pipe(Effect.provide(LayerLive)), |
| 45 | ); |
| 46 | const stack = beforeAll(destroy(Stack).pipe(Effect.andThen(deploy(Stack)))); |
| 47 | afterAll(destroy(Stack)); |
| 48 | |
| 49 | test( |
| 50 | "Effect constructor and make Layer entrypoints invoke across a URL-only binding", |
| 51 | Effect.gen(function* () { |
| 52 | const urls = yield* stack; |
| 53 | const client = yield* HttpClient.HttpClient; |
| 54 | expect(yield* (yield* client.get(urls.constructor)).text).toBe("constructor"); |
| 55 | expect(yield* (yield* client.get(urls.layer)).text).toBe("layer:constructor"); |
| 56 | }), |
| 57 | { |
| 58 | tags: ["provider:neon", "provider:neon:function", "provider:neon:project", "live"], |
| 59 | timeout: 120_000, |
| 60 | }, |
| 61 | ); |
| 62 | |
| 63 | test( |
| 64 | "bare and Hono native exports preserve application authentication", |
| 65 | Effect.gen(function* () { |
| 66 | const urls = yield* stack; |
| 67 | const client = yield* HttpClient.HttpClient; |
| 68 | expect(yield* (yield* client.get(urls.bare)).text).toBe("bare-v2"); |
| 69 | expect(yield* (yield* client.get(urls.hono)).text).toBe("hono"); |
| 70 | expect((yield* client.get(`${urls.hono}private`)).status).toBe(401); |
| 71 | const authorized = yield* client.execute( |
| 72 | HttpClientRequest.get(`${urls.hono}private`).pipe( |
| 73 | HttpClientRequest.setHeader("authorization", "Bearer test-caller"), |
| 74 | ), |
| 75 | ); |
| 76 | expect(yield* authorized.text).toBe("authorized"); |
| 77 | }), |
| 78 | { |
| 79 | tags: ["provider:neon", "provider:neon:function", "provider:neon:project", "live"], |
| 80 | timeout: 120_000, |
| 81 | }, |
| 82 | ); |
| 83 | |
| 84 | test.provider( |
| 85 | "Function logs expose only the requested Function's application records", |
| 86 | () => |
| 87 | Effect.gen(function* () { |
| 88 | const { host, bare } = yield* stack; |
| 89 | const service = `neon-function/${host.slug}`; |
| 90 | const record = (second: number): SDK.ProjectBranchLogRecord => ({ |
| 91 | timestamp: `2026-09-17T00:00:0${second}Z`, |
| 92 | message: `fixture-${second}`, |
| 93 | source: "function", |
| 94 | service_name: service, |
| 95 | entity_id: "opaque-runtime-instance", |
| 96 | attributes: {}, |
| 97 | }); |
| 98 | const requests: Record<string, unknown>[] = []; |
| 99 | const fixture = (respond: (page: number) => SDK.ProjectBranchLogsQueryResponse) => |
| 100 | HttpClient.make((request) => |
| 101 | Effect.sync(() => { |
| 102 | if (request.body._tag !== "Uint8Array") throw new Error("Expected JSON log query"); |
| 103 | requests.push(JSON.parse(new TextDecoder().decode(request.body.body))); |
| 104 | return HttpClientResponse.fromWeb(request, Response.json(respond(requests.length))); |
| 105 | }), |
| 106 | ); |
| 107 | const selected = yield* FunctionLogs(host, { limit: 3 }).pipe( |
| 108 | Effect.provideService( |
| 109 | HttpClient.HttpClient, |
| 110 | fixture((page) => |
| 111 | page === 1 |
| 112 | ? { |
| 113 | logs: [ |
| 114 | record(3), |
| 115 | { |
| 116 | ...record(3), |
| 117 | entity_id: host.functionId, |
| 118 | service_name: "neon-function/another", |
| 119 | }, |
| 120 | { ...record(3), service_name: undefined }, |
| 121 | ], |
| 122 | is_truncated: true, |
| 123 | next_cursor: "older", |
| 124 | } |
| 125 | : { logs: [record(2), record(1)], is_truncated: false }, |
| 126 | ), |
| 127 | ), |
| 128 | Effect.provide(SDK.fromApiKey({ apiKey: Redacted.make("fixture-key") })), |
| 129 | ); |
| 130 | expect(selected.map((line) => line.message)).toEqual(["fixture-1", "fixture-2", "fixture-3"]); |
| 131 | expect(requests.length).toBe(2); |
| 132 | expect(requests[0]).toMatchObject({ |
| 133 | source: "function", |
| 134 | service_name: service, |
| 135 | sort_order: "desc", |
| 136 | since: "1h", |
| 137 | limit: 3, |
| 138 | }); |
| 139 | expect(typeof requests[0].end_time).toBe("string"); |
| 140 | expect(requests[1]).toEqual({ ...requests[0], cursor: "older" }); |
| 141 | for (const mode of ["repeated", "missing", "bounded"] as const) { |
| 142 | requests.length = 0; |
| 143 | const result = yield* FunctionLogs(host, { limit: 3 }).pipe( |
| 144 | Effect.provideService( |
| 145 | HttpClient.HttpClient, |
| 146 | fixture((page) => ({ |
| 147 | logs: [], |
| 148 | is_truncated: true, |
| 149 | next_cursor: |
| 150 | mode === "missing" ? undefined : mode === "repeated" ? "same" : `page-${page}`, |
| 151 | })), |
| 152 | ), |
| 153 | Effect.provide(SDK.fromApiKey({ apiKey: Redacted.make("fixture-key") })), |
| 154 | Effect.as("unexpected-success"), |
| 155 | Effect.catchTag("FunctionLogQueryError", (error) => Effect.succeed(error.reason)), |
| 156 | ); |
| 157 | expect(result).toBe(mode === "bounded" ? "pagination-limit" : "invalid-cursor"); |
| 158 | expect(requests.length).toBe(mode === "bounded" ? 8 : mode === "missing" ? 1 : 2); |
| 159 | } |
| 160 | const client = yield* HttpClient.HttpClient; |
| 161 | const since = yield* Effect.sync(() => new Date()); |
| 162 | expect(yield* client.get(bare).pipe(Effect.flatMap((response) => response.text))).toBe( |
| 163 | "bare-v2", |
| 164 | ); |
| 165 | expect(yield* client.get(host.url).pipe(Effect.flatMap((response) => response.text))).toBe( |
| 166 | "hono", |
| 167 | ); |
| 168 | const marker = "alchemy-neon-hono-log-probe"; |
| 169 | // Log ingestion is eventually consistent; poll up to 2 minutes. |
| 170 | const lines = yield* FunctionLogs(host, { limit: 100, since }).pipe( |
| 171 | Effect.repeat({ |
| 172 | schedule: Schedule.spaced("5 seconds"), |
| 173 | until: (lines) => lines.some((line) => line.message.includes(marker)), |
| 174 | }), |
| 175 | Effect.timeout("2 minutes"), |
| 176 | ); |
| 177 | const observed = yield* SDK.queryProjectBranchLogs({ |
| 178 | project_id: host.projectId, |
| 179 | branch_id: host.branchId, |
| 180 | source: "function", |
| 181 | service_name: service, |
| 182 | start_time: since.toISOString(), |
| 183 | sort_order: "desc", |
| 184 | limit: 1000, |
| 185 | }); |
| 186 | yield* Effect.logInfo( |
| 187 | JSON.stringify({ |
| 188 | functionLogMetadata: { |
| 189 | records: observed.logs.length, |
| 190 | markerRecords: observed.logs.filter((line) => line.message.includes(marker)).length, |
| 191 | serviceMatches: observed.logs.filter((line) => line.service_name === service).length, |
| 192 | functionSources: observed.logs.filter((line) => line.source === "function").length, |
| 193 | entityMatchesFunctionId: observed.logs.filter( |
| 194 | (line) => line.entity_id === host.functionId, |
| 195 | ).length, |
| 196 | entityMatchesSlug: observed.logs.filter((line) => line.entity_id === host.slug).length, |
| 197 | entityPresent: observed.logs.filter((line) => line.entity_id !== undefined).length, |
| 198 | truncated: observed.is_truncated, |
| 199 | }, |
| 200 | }), |
| 201 | ); |
| 202 | expect(lines.some((line) => line.message.includes(marker))).toBe(true); |
| 203 | const functions = yield* SDK.listProjectBranchFunctions({ |
| 204 | project_id: host.projectId, |
| 205 | branch_id: host.branchId, |
| 206 | }); |
| 207 | const sibling = functions.functions.find((fn) => fn.invocation_url === bare); |
| 208 | if (!sibling) |
| 209 | return yield* Effect.fail(new Error("Missing sibling Function for log isolation check")); |
| 210 | // The sibling's records ingest independently; poll until they appear. |
| 211 | const unrelated = yield* FunctionLogs( |
| 212 | { ...host, functionId: sibling.id, slug: sibling.slug }, |
| 213 | { limit: 100, since }, |
| 214 | ).pipe( |
| 215 | Effect.repeat({ |
| 216 | schedule: Schedule.spaced("5 seconds"), |
| 217 | until: (lines) => lines.length > 0, |
| 218 | }), |
| 219 | Effect.timeout("2 minutes"), |
| 220 | ); |
| 221 | expect(unrelated.length).toBeGreaterThan(0); |
| 222 | expect(unrelated.some((line) => line.message.includes(marker))).toBe(false); |
| 223 | }), |
| 224 | { |
| 225 | tags: ["provider:neon", "provider:neon:function", "provider:neon:project", "live"], |
| 226 | timeout: 300_000, |
| 227 | }, |
| 228 | ); |