MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / withLocalServer

Function withLocalServer

e2e/local/local-server.ts:38–123  ·  view source on GitHub ↗
(
  cli: CliSurface,
  runDir: string,
  body: (server: ServerHandle) => Effect.Effect<void>,
  options?: {
    /** Extra env merged into the `executor web` process, e.g. to force a
     *  published-version signal for the update check. */
    readonly env?: Record<string, string>;
  },
)

Source from the content-addressed store, hash-verified

36 * server.
37 */
38export const withLocalServer = (
39 cli: CliSurface,
40 runDir: string,
41 body: (server: ServerHandle) => Effect.Effect<void>,
42 options?: {
43 /** Extra env merged into the `executor web` process, e.g. to force a
44 * published-version signal for the update check. */
45 readonly env?: Record<string, string>;
46 },
47): Effect.Effect<void> =>
48 Effect.gen(function* () {
49 const dataDir = mkdtempSync(join(tmpdir(), "executor-local-e2e-"));
50
51 let publishUrl!: (url: string) => void;
52 const urlReady = new Promise<string>((res) => {
53 publishUrl = res;
54 });
55 let signalBodyDone!: () => void;
56 const bodyDone = new Promise<void>((res) => {
57 signalBodyDone = res;
58 });
59
60 yield* Effect.all(
61 [
62 cli.session(
63 ["bun", "run", "dev:cli", "web", "--foreground", "--port", "0"],
64 async (term) => {
65 markRecordingStart(runDir, "terminal");
66 markFocus(runDir, "terminal");
67 // Capture the latest rendered screen on every poll so a boot that
68 // never prints the URL is still diagnosable: `waitUntil` rejects
69 // with a tail-less "timed out" on its deadline, so without this the
70 // failure tells us nothing about how far boot actually got.
71 let lastText = "";
72 const snapshot = await term.screen
73 .waitUntil(
74 (current) => {
75 lastText = current.text;
76 return TOKEN_URL.test(current.text);
77 },
78 // A cold `executor web` runs vite's optimizeDeps before it prints
79 // the URL; 240s leaves headroom for isolated CI cold boots. Kept
80 // BELOW each scenario's test timeout (300s) so that on a slow or
81 // wedged boot THIS error surfaces, with the terminal tail, rather
82 // than racing vitest's generic timeout at the same deadline.
83 { timeoutMs: 240_000 },
84 )
85 .catch((cause: unknown) => {
86 throw new Error(
87 `executor web --foreground printed no ?_token URL within 240s (${String(cause)}):\n${lastText.slice(-600)}`,
88 );
89 });
90 const url = TOKEN_URL.exec(snapshot.text)?.[0];
91 if (!url) {
92 throw new Error(
93 `executor web --foreground printed no ?_token URL:\n${snapshot.text.slice(-600)}`,
94 );
95 }

Calls 6

markRecordingStartFunction · 0.90
markFocusFunction · 0.90
execMethod · 0.80
waitUntilMethod · 0.65
getMethod · 0.65
syncMethod · 0.65

Tested by

no test coverage detected