|
| 1 | +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { describe, expect, it } from "@effect/vitest"; |
| 5 | +import * as Effect from "effect/Effect"; |
| 6 | + |
| 7 | +import { |
| 8 | + createControlPlaneRuntime, |
| 9 | +} from "./index"; |
| 10 | +import { withControlPlaneClient } from "./test-http-client"; |
| 11 | + |
| 12 | +const writeProjectConfig = ( |
| 13 | + workspaceRoot: string, |
| 14 | + config: Record<string, unknown>, |
| 15 | +) => { |
| 16 | + const configDirectory = join(workspaceRoot, ".executor"); |
| 17 | + mkdirSync(configDirectory, { recursive: true }); |
| 18 | + writeFileSync( |
| 19 | + join(configDirectory, "executor.jsonc"), |
| 20 | + `${JSON.stringify(config, null, 2)}\n`, |
| 21 | + "utf8", |
| 22 | + ); |
| 23 | +}; |
| 24 | + |
| 25 | +const makeRuntime = (config: Record<string, unknown>) => { |
| 26 | + const workspaceRoot = mkdtempSync(join(tmpdir(), "executor-runtime-config-")); |
| 27 | + writeProjectConfig(workspaceRoot, config); |
| 28 | + |
| 29 | + return Effect.acquireRelease( |
| 30 | + createControlPlaneRuntime({ |
| 31 | + workspaceRoot, |
| 32 | + homeConfigPath: join(workspaceRoot, ".executor-home.jsonc"), |
| 33 | + homeStateDirectory: join(workspaceRoot, ".executor-home-state"), |
| 34 | + }), |
| 35 | + (runtime) => Effect.promise(() => runtime.close()).pipe(Effect.orDie), |
| 36 | + ); |
| 37 | +}; |
| 38 | + |
| 39 | +describe("execution runtime config", () => { |
| 40 | + it.scoped("defaults to QuickJS when no runtime is configured", () => |
| 41 | + Effect.gen(function* () { |
| 42 | + const runtime = yield* makeRuntime({ |
| 43 | + sources: {}, |
| 44 | + }); |
| 45 | + const installation = runtime.localInstallation; |
| 46 | + |
| 47 | + const execution = yield* withControlPlaneClient( |
| 48 | + { |
| 49 | + runtime, |
| 50 | + accountId: installation.accountId, |
| 51 | + }, |
| 52 | + (client) => |
| 53 | + client.executions.create({ |
| 54 | + path: { |
| 55 | + workspaceId: installation.workspaceId, |
| 56 | + }, |
| 57 | + payload: { |
| 58 | + code: 'await fetch("https://example.com"); return 1;', |
| 59 | + }, |
| 60 | + }), |
| 61 | + ); |
| 62 | + |
| 63 | + expect(execution.execution.status).toBe("failed"); |
| 64 | + expect(execution.execution.errorText).toContain( |
| 65 | + "fetch is disabled in QuickJS executor", |
| 66 | + ); |
| 67 | + }), |
| 68 | + 60_000, |
| 69 | + ); |
| 70 | + |
| 71 | + it.scoped("uses the SES runtime when configured in executor.jsonc", () => |
| 72 | + Effect.gen(function* () { |
| 73 | + const runtime = yield* makeRuntime({ |
| 74 | + runtime: "ses", |
| 75 | + sources: {}, |
| 76 | + }); |
| 77 | + const installation = runtime.localInstallation; |
| 78 | + |
| 79 | + const execution = yield* withControlPlaneClient( |
| 80 | + { |
| 81 | + runtime, |
| 82 | + accountId: installation.accountId, |
| 83 | + }, |
| 84 | + (client) => |
| 85 | + client.executions.create({ |
| 86 | + path: { |
| 87 | + workspaceId: installation.workspaceId, |
| 88 | + }, |
| 89 | + payload: { |
| 90 | + code: 'await fetch("https://example.com"); return 1;', |
| 91 | + }, |
| 92 | + }), |
| 93 | + ); |
| 94 | + |
| 95 | + expect(execution.execution.status).toBe("failed"); |
| 96 | + expect(execution.execution.errorText).toContain( |
| 97 | + "fetch is disabled in SES executor", |
| 98 | + ); |
| 99 | + }), |
| 100 | + 60_000, |
| 101 | + ); |
| 102 | + |
| 103 | + it.scoped("uses the Deno runtime when configured in executor.jsonc", () => |
| 104 | + Effect.gen(function* () { |
| 105 | + const runtime = yield* makeRuntime({ |
| 106 | + runtime: "deno", |
| 107 | + sources: {}, |
| 108 | + }); |
| 109 | + const installation = runtime.localInstallation; |
| 110 | + |
| 111 | + const execution = yield* withControlPlaneClient( |
| 112 | + { |
| 113 | + runtime, |
| 114 | + accountId: installation.accountId, |
| 115 | + }, |
| 116 | + (client) => |
| 117 | + client.executions.create({ |
| 118 | + path: { |
| 119 | + workspaceId: installation.workspaceId, |
| 120 | + }, |
| 121 | + payload: { |
| 122 | + code: 'return typeof Deno !== "undefined";', |
| 123 | + }, |
| 124 | + }), |
| 125 | + ); |
| 126 | + |
| 127 | + if (execution.execution.status === "completed") { |
| 128 | + expect(execution.execution.resultJson).toBe("true"); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + expect(execution.execution.status).toBe("failed"); |
| 133 | + expect(execution.execution.errorText).toContain("Install Deno or set DENO_BIN"); |
| 134 | + }), |
| 135 | + 60_000, |
| 136 | + ); |
| 137 | +}); |
0 commit comments