The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { Function } from "@alchemy.run/neon/Function"; |
| 2 | import { Project } from "@alchemy.run/neon/Project"; |
| 3 | import { providers } from "@alchemy.run/neon/Providers"; |
| 4 | import { packageWebsiteArtifact } from "@alchemy.run/neon/Website/Artifact"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import { getProject } from "@distilled.cloud/neon"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as HttpClient from "effect/http/HttpClient"; |
| 11 | import * as Path from "effect/Path"; |
| 12 | import { bodyContaining, buildPortableExample, exampleRoot } from "./Fixture.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: providers() }); |
| 15 | |
| 16 | for (const slug of ["nextjs", "vocs"] as const) |
| 17 | test.provider.skipIf(!process.env.NEON_WEBSITE_FRESH)( |
| 18 | `fresh ${slug} portable artifact serves production requests`, |
| 19 | (stack) => |
| 20 | Effect.gen(function* () { |
| 21 | yield* stack.destroy(); |
| 22 | const fs = yield* FileSystem.FileSystem; |
| 23 | const path = yield* Path.Path; |
| 24 | const root = yield* exampleRoot(slug); |
| 25 | const next = slug === "nextjs"; |
| 26 | const pixel = path.join(root, "public/neon-artifact-pixel.png"); |
| 27 | if (next) { |
| 28 | const original = (yield* fs.exists(pixel)) ? yield* fs.readFile(pixel) : undefined; |
| 29 | yield* Effect.addFinalizer(() => |
| 30 | (original ? fs.writeFile(pixel, original) : fs.remove(pixel, { force: true })).pipe( |
| 31 | Effect.orDie, |
| 32 | ), |
| 33 | ); |
| 34 | yield* fs.writeFile( |
| 35 | pixel, |
| 36 | yield* Effect.sync(() => |
| 37 | Buffer.from( |
| 38 | "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=", |
| 39 | "base64", |
| 40 | ), |
| 41 | ), |
| 42 | ); |
| 43 | } |
| 44 | const artifact = yield* packageWebsiteArtifact(yield* buildPortableExample(slug, root)); |
| 45 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 46 | const zip = path.join(directory, "site.zip"); |
| 47 | yield* fs.writeFile(zip, artifact.archive); |
| 48 | const site = yield* stack.deploy( |
| 49 | Effect.gen(function* () { |
| 50 | const project = yield* Project("Project", { |
| 51 | region: "aws-us-east-2", |
| 52 | }); |
| 53 | return yield* Function("Site", { |
| 54 | project, |
| 55 | artifact: { zip }, |
| 56 | env: { NODE_ENV: "production" }, |
| 57 | }); |
| 58 | }), |
| 59 | ); |
| 60 | const url = site.url.replace(/\/$/, ""); |
| 61 | yield* Effect.logInfo(`Fresh ${slug} artifact: ${url}`); |
| 62 | const html = yield* bodyContaining(`${url}/`, "Neon"); |
| 63 | for (const method of ["GET", "HEAD"] as const) { |
| 64 | const response = yield* method === "HEAD" |
| 65 | ? HttpClient.head(`${url}/example.json`) |
| 66 | : HttpClient.get(`${url}/example.json`); |
| 67 | expect(response.status).toBe(200); |
| 68 | const body = yield* response.text; |
| 69 | if (method === "HEAD") expect(body).toBe(""); |
| 70 | else expect(body).toContain("framework"); |
| 71 | } |
| 72 | const assets = [...html.matchAll(/(?:src|href)="([^" ]+\.(?:js|css)(?:\?[^" ]*)?)"/g)] |
| 73 | .map((match) => match[1]!) |
| 74 | .filter((url) => url.startsWith("/")); |
| 75 | expect(assets.length).toBeGreaterThan(0); |
| 76 | for (const asset of assets.slice(0, 3)) { |
| 77 | const response = yield* HttpClient.get(`${url}${asset}`); |
| 78 | expect(response.status).toBe(200); |
| 79 | expect((yield* response.arrayBuffer).byteLength).toBeGreaterThan(0); |
| 80 | } |
| 81 | if (next) { |
| 82 | const image = yield* HttpClient.get( |
| 83 | `${url}/_next/image?url=%2Fneon-artifact-pixel.png&w=64&q=75`, |
| 84 | { headers: { accept: "image/webp" } }, |
| 85 | ); |
| 86 | expect(image.status).toBe(200); |
| 87 | expect(image.headers["content-type"]).toContain("image/webp"); |
| 88 | expect((yield* image.arrayBuffer).byteLength).toBeGreaterThan(0); |
| 89 | const stream = yield* HttpClient.get(`${url}/api/stream`); |
| 90 | expect(stream.status).toBe(200); |
| 91 | expect(stream.headers["content-type"]).toContain("text/event-stream"); |
| 92 | expect(yield* stream.text).toBe("data: first\n\ndata: second\n\n"); |
| 93 | } else yield* bodyContaining(`${url}/guide`, "Guide"); |
| 94 | yield* stack.destroy(); |
| 95 | expect( |
| 96 | yield* getProject({ project_id: site.projectId }).pipe( |
| 97 | Effect.as(false), |
| 98 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 99 | ), |
| 100 | ).toBe(true); |
| 101 | }).pipe(Effect.scoped), |
| 102 | { |
| 103 | tags: [ |
| 104 | "provider:neon", |
| 105 | "provider:neon:function", |
| 106 | "provider:neon:project", |
| 107 | "provider:neon:website", |
| 108 | "live", |
| 109 | ], |
| 110 | timeout: 120_000, |
| 111 | }, |
| 112 | ); |