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 { expect } from "@alchemy.run/test"; |
| 2 | import { initialCwd } from "alchemy/Util/Node"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Path from "effect/Path"; |
| 6 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as Semaphore from "effect/Semaphore"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | |
| 11 | const command = Effect.fn( |
| 12 | function* (args: string[]) { |
| 13 | const child = yield* ChildProcess.make("terminal-browser", args, { |
| 14 | env: { ...process.env, NODE_OPTIONS: undefined }, |
| 15 | }); |
| 16 | const [code, stdout, stderr] = yield* Effect.all( |
| 17 | [ |
| 18 | child.exitCode, |
| 19 | child.stdout.pipe(Stream.decodeText, Stream.mkString), |
| 20 | child.stderr.pipe(Stream.decodeText, Stream.mkString), |
| 21 | ], |
| 22 | { concurrency: "unbounded" }, |
| 23 | ); |
| 24 | if (code !== 0) return yield* Effect.fail(new Error(`Browser command failed: ${stderr}`)); |
| 25 | return stdout; |
| 26 | }, |
| 27 | Effect.scoped, |
| 28 | Effect.timeout("10 seconds"), |
| 29 | ); |
| 30 | |
| 31 | const browsers = Semaphore.makeUnsafe(1); |
| 32 | |
| 33 | /** Opt-in real terminal-browser interactions against each deployed or native app. */ |
| 34 | export const browserRoundtrip = Effect.fn( |
| 35 | function* (url: string, slug: string) { |
| 36 | if (process.env.NEON_WEBSITE_BROWSER !== "1") return; |
| 37 | const fs = yield* FileSystem.FileSystem; |
| 38 | const path = yield* Path.Path; |
| 39 | const screenshots = path.join(initialCwd, ".alchemy", "log", "website-browser"); |
| 40 | yield* fs.makeDirectory(screenshots, { recursive: true }); |
| 41 | const opened = yield* command([ |
| 42 | "new-tab", |
| 43 | slug === "vocs" ? `${url.replace(/\/+$/, "")}/counter` : url, |
| 44 | ]); |
| 45 | const browser = yield* Effect.try( |
| 46 | () => |
| 47 | JSON.parse(opened) as { |
| 48 | key: string; |
| 49 | openedTab?: number; |
| 50 | tabs?: { id: number; active: boolean }[]; |
| 51 | }, |
| 52 | ); |
| 53 | const openedTab = browser.openedTab ?? browser.tabs?.find((tab) => tab.active)?.id; |
| 54 | if (openedTab === undefined) |
| 55 | return yield* Effect.fail(new Error("Browser did not report its active tab")); |
| 56 | const tab = { key: browser.key, openedTab }; |
| 57 | const action = (...args: string[]) => |
| 58 | command(["action", "--browser", tab.key, "--tab", String(tab.openedTab), "--", ...args]); |
| 59 | yield* Effect.addFinalizer(() => |
| 60 | action("eval", "location.href = 'about:blank'").pipe( |
| 61 | Effect.andThen(action("close")), |
| 62 | Effect.andThen( |
| 63 | command(["action", "--browser", tab.key, "--tab", String(tab.openedTab), "done"]), |
| 64 | ), |
| 65 | Effect.ignore, |
| 66 | ), |
| 67 | ); |
| 68 | for (const [width, height] of [ |
| 69 | [1280, 900], |
| 70 | [390, 844], |
| 71 | ]) { |
| 72 | yield* action("set", "viewport", String(width), String(height)); |
| 73 | yield* action("reload"); |
| 74 | const label = slug === "foldkit" ? "+" : "count: 0"; |
| 75 | const snapshot = yield* action("snapshot", "-i").pipe( |
| 76 | Effect.repeat({ |
| 77 | schedule: Schedule.spaced("500 millis"), |
| 78 | times: 8, |
| 79 | until: (text) => text.includes(label), |
| 80 | }), |
| 81 | ); |
| 82 | expect(snapshot).toContain(label); |
| 83 | yield* action("find", "role", "button", "click", "--name", label, "--exact"); |
| 84 | if (slug === "foldkit") { |
| 85 | expect(yield* action("get", "text", "#count")).toContain("1"); |
| 86 | yield* action("find", "role", "button", "click", "--name", "Reset", "--exact"); |
| 87 | expect(yield* action("get", "text", "#count")).toContain("0"); |
| 88 | } else { |
| 89 | expect(yield* action("snapshot", "-i")).toContain("count: 1"); |
| 90 | if (!["vocs", "waku"].includes(slug)) { |
| 91 | expect(snapshot).toContain("Load greeting"); |
| 92 | yield* action("find", "role", "button", "click", "--name", "Load greeting", "--exact"); |
| 93 | expect( |
| 94 | yield* action( |
| 95 | "get", |
| 96 | "text", |
| 97 | slug === "nextjs" ? 'section [role="status"]' : '[role="status"]', |
| 98 | ).pipe( |
| 99 | Effect.repeat({ |
| 100 | schedule: Schedule.spaced("500 millis"), |
| 101 | times: 8, |
| 102 | until: (text) => text.includes("Hello from"), |
| 103 | }), |
| 104 | ), |
| 105 | ).toContain("Hello from"); |
| 106 | } |
| 107 | } |
| 108 | if (slug === "nextjs") { |
| 109 | yield* action("fill", "#name", `Neon visitor ${width}`); |
| 110 | yield* action("find", "role", "button", "click", "--name", "Submit name", "--exact"); |
| 111 | expect( |
| 112 | yield* action("get", "text", "body").pipe( |
| 113 | Effect.repeat({ |
| 114 | schedule: Schedule.spaced("500 millis"), |
| 115 | times: 8, |
| 116 | until: (text) => text.includes(`Submitted: Neon visitor ${width}`), |
| 117 | }), |
| 118 | ), |
| 119 | ).toContain(`Submitted: Neon visitor ${width}`); |
| 120 | yield* action("reload"); |
| 121 | expect(yield* action("get", "text", "body")).toContain(`Submitted: Neon visitor ${width}`); |
| 122 | } |
| 123 | expect( |
| 124 | yield* action("eval", "document.documentElement.scrollWidth <= window.innerWidth"), |
| 125 | ).toContain("true"); |
| 126 | yield* action( |
| 127 | "screenshot", |
| 128 | path.join( |
| 129 | screenshots, |
| 130 | `${slug}-${url.startsWith("https:") ? "live" : "local"}-${width}.png`, |
| 131 | ), |
| 132 | ); |
| 133 | if (["astro", "nuxt", "sveltekit", "waku"].includes(slug)) { |
| 134 | const route = slug === "astro" ? "/about/" : "/about"; |
| 135 | yield* action("click", `a[href="${route}"]`); |
| 136 | expect(yield* action("get", "url")).toContain("/about"); |
| 137 | yield* action("reload"); |
| 138 | expect(yield* action("get", "text", "body")).toContain("prerendered"); |
| 139 | yield* action("back"); |
| 140 | } |
| 141 | if (slug === "vocs") { |
| 142 | const base = url.replace(/\/+$/, ""); |
| 143 | for (const [route, heading] of [ |
| 144 | ["/guide", "Deployment guide"], |
| 145 | ["/", "Alchemy with Vocs"], |
| 146 | ]) { |
| 147 | yield* action("click", `nav[aria-label="Pagination"] a[href="${route}"]`); |
| 148 | const headingText = action("get", "text", "h1").pipe( |
| 149 | Effect.repeat({ |
| 150 | schedule: Schedule.spaced("500 millis"), |
| 151 | until: (text) => text.includes(heading), |
| 152 | }), |
| 153 | Effect.timeout("10 seconds"), |
| 154 | ); |
| 155 | expect(yield* headingText).toContain(heading); |
| 156 | yield* action("reload"); |
| 157 | expect(yield* headingText).toContain(heading); |
| 158 | } |
| 159 | yield* action("back"); |
| 160 | expect(yield* action("get", "url")).toContain("/guide"); |
| 161 | yield* action("eval", `location.href = ${JSON.stringify(`${base}/counter`)}`); |
| 162 | } |
| 163 | yield* Effect.logInfo(`Website browser ${slug} ${width}x${height}: passed`); |
| 164 | } |
| 165 | }, |
| 166 | Effect.scoped, |
| 167 | browsers.withPermit, |
| 168 | ); |