Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions pkgs/mcpx/internal/e2e/effect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/developing-today/code/pkgs/mcpx/internal/settings"
"github.com/developing-today/code/pkgs/mcpx/internal/testsupport"
)

// Effect probes, grouped by surface (#1971, Approach C).
Expand Down Expand Up @@ -100,6 +101,11 @@ var implementedEffectProbes = map[string]string{
"protoTasks.maxPerRequestor": "TestProtoTasksMaxPerRequestorCapsUnfinishedTasksFromTheEnvironment",
"protoTasks.perMinute": "TestProtoTasksPerMinuteWindowsTaskReadsFromTheEnvironment",
"events.history": "TestEventsHistoryBoundsTheReplay",
"script.runtime": "TestScriptRuntimeSelectsTheRuntime",
"script.runtimeOrder": "TestScriptRuntimeOrderRanksTheAutoPick",
"script.permissions": "TestScriptPermissionsSandboxesNode",
"script.profiles": "TestScriptProfilesDefinesAProfile",
"script.runtimes": "TestScriptRuntimesDeclaresARuntime",
"script.before": "TestScriptPhasesRunFromTheEnvironment",
"script.onSuccess": "TestScriptPhasesRunFromTheEnvironment",
"script.onError": "TestScriptPhasesRunFromTheEnvironment",
Expand Down Expand Up @@ -217,6 +223,11 @@ var effectProbeFuncs = map[string]func(*testing.T){
"TestProtoTasksMaxPerRequestorCapsUnfinishedTasksFromTheEnvironment": TestProtoTasksMaxPerRequestorCapsUnfinishedTasksFromTheEnvironment,
"TestProtoTasksPerMinuteWindowsTaskReadsFromTheEnvironment": TestProtoTasksPerMinuteWindowsTaskReadsFromTheEnvironment,
"TestEventsHistoryBoundsTheReplay": TestEventsHistoryBoundsTheReplay,
"TestScriptRuntimeSelectsTheRuntime": TestScriptRuntimeSelectsTheRuntime,
"TestScriptRuntimeOrderRanksTheAutoPick": TestScriptRuntimeOrderRanksTheAutoPick,
"TestScriptPermissionsSandboxesNode": TestScriptPermissionsSandboxesNode,
"TestScriptProfilesDefinesAProfile": TestScriptProfilesDefinesAProfile,
"TestScriptRuntimesDeclaresARuntime": TestScriptRuntimesDeclaresARuntime,
"TestScriptPhasesRunFromTheEnvironment": TestScriptPhasesRunFromTheEnvironment,
"TestHooksRunOnTheirEvent": TestHooksRunOnTheirEvent,
"TestHooksTimeoutStopsASlowHook": TestHooksTimeoutStopsASlowHook,
Expand Down Expand Up @@ -2138,6 +2149,106 @@ func TestScriptPhasesRunFromTheEnvironment(t *testing.T) {
}
}

// runtimeOf runs a script with --json and returns the runtime it reports.
func runtimeOf(t *testing.T, e *env, args ...string) string {
t.Helper()
var doc struct {
Runtime string `json:"runtime"`
}
out := e.run(append(append([]string{"exec", "--json"}, args...), `console.log("hi")`)...)
if err := json.Unmarshal([]byte(jsonOf(t, out)), &doc); err != nil {
t.Fatalf("%v\n%s", err, out)
}
return doc.Runtime
}

// TestScriptRuntimeSelectsTheRuntime is the effect probe for
// script.runtime: scripts run on bun by default and on node once the
// environment names it.
func TestScriptRuntimeSelectsTheRuntime(t *testing.T) {
testsupport.RequireBinary(t, "node")
e := newEnv(t, oneServer)
if rt := runtimeOf(t, e); rt != "bun" {
t.Errorf("the default script.runtime should pick bun, got %q", rt)
}
e.setenv("MCPX_SCRIPT_RUNTIME=node")
if rt := runtimeOf(t, e); rt != "node" {
t.Errorf("script.runtime=node should run on node, got %q", rt)
}
}

// TestScriptRuntimeOrderRanksTheAutoPick is the effect probe for
// script.runtimeOrder: the automatic pick is bun by default and node once
// the environment ranks node first.
func TestScriptRuntimeOrderRanksTheAutoPick(t *testing.T) {
testsupport.RequireBinary(t, "node")
e := newEnv(t, oneServer)
if rt := runtimeOf(t, e); rt != "bun" {
t.Errorf("the default script.runtimeOrder should pick bun, got %q", rt)
}
e.setenv("MCPX_SCRIPT_RUNTIME_ORDER=node,bun")
if rt := runtimeOf(t, e); rt != "node" {
t.Errorf("script.runtimeOrder=node,bun should pick node, got %q", rt)
}
}

// nodeWrite runs, on node, a script that tries to write a file and prints
// WROTE or DENIED.
func nodeWrite(t *testing.T, e *env, args ...string) string {
t.Helper()
target := filepath.Join(t.TempDir(), "w")
probe := `import { writeFileSync } from "node:fs";
try { writeFileSync(` + jsonQuote(target) + `, "1"); console.log("WROTE"); } catch (e) { console.log("DENIED " + e.code); }`
out, _ := e.try(append(append([]string{"exec", "--runtime", "node"}, args...), probe)...)
return out
}

// TestScriptPermissionsSandboxesNode is the effect probe for
// script.permissions: node writes freely at the default, and the readnet
// profile named in the environment makes node refuse the write.
func TestScriptPermissionsSandboxesNode(t *testing.T) {
testsupport.RequireBinary(t, "node")
e := newEnv(t, oneServer)
if out := nodeWrite(t, e); !strings.Contains(out, "WROTE") {
t.Errorf("the default script.permissions should let node write:\n%s", out)
}
e.setenv("MCPX_SCRIPT_PERMISSIONS=readnet")
if out := nodeWrite(t, e); !strings.Contains(out, "DENIED ERR_ACCESS_DENIED") {
t.Errorf("script.permissions=readnet should make node refuse the write:\n%s", out)
}
}

// TestScriptProfilesDefinesAProfile is the effect probe for script.profiles:
// --permissions ro names no profile by default, and once the environment
// defines ro with node's read-only flags, node refuses the write under it.
func TestScriptProfilesDefinesAProfile(t *testing.T) {
testsupport.RequireBinary(t, "node")
e := newEnv(t, oneServer)
if out := nodeWrite(t, e, "--permissions", "ro"); strings.Contains(out, "WROTE") || strings.Contains(out, "DENIED") {
t.Errorf("with no profile ro defined, the script should not run:\n%s", out)
}
e.setenv(`MCPX_SCRIPT_PROFILES={"ro":{"node":["--permission","--allow-fs-read=*"]}}`)
if out := nodeWrite(t, e, "--permissions", "ro"); !strings.Contains(out, "DENIED ERR_ACCESS_DENIED") {
t.Errorf("script.profiles defining ro should make node refuse the write:\n%s", out)
}
}

// TestScriptRuntimesDeclaresARuntime is the effect probe for
// script.runtimes: --runtime engine is unknown by default, and runs on
// node once the environment declares engine as a node binary.
func TestScriptRuntimesDeclaresARuntime(t *testing.T) {
node := testsupport.RequireBinary(t, "node")
e := newEnv(t, oneServer)
src := `console.log("kind=" + process.env.MCPX_RUNTIME)`
if out, err := e.try("exec", "--runtime", "engine", src); err == nil || strings.Contains(out, "kind=node") {
t.Errorf("with nothing declared, --runtime engine should fail: %v\n%s", err, out)
}
e.setenv(`MCPX_SCRIPT_RUNTIMES={"engine":{"kind":"node","bin":` + jsonQuote(node) + `}}`)
if out, err := e.try("exec", "--runtime", "engine", src); err != nil || !strings.Contains(out, "kind=node") {
t.Errorf("script.runtimes should declare engine as node: %v\n%s", err, out)
}
}

// TestDaemonAddressIsReachableFromTheEnvironment is the effect probe
// for daemon.address (#8468 chain 1). The exemption it replaced said
// a bind address is a network identity the harness cannot vary
Expand Down
5 changes: 0 additions & 5 deletions pkgs/mcpx/internal/e2e/feature_coverage_exemptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ setting script.importsNetwork no integration-test exercise yet; burn-down #8474
setting script.importsOnDynamic no integration-test exercise yet; burn-down #8474 (settings script family); needs an EffectProbes entry + e2e test
setting script.importsOnMissing no integration-test exercise yet; burn-down #8474 (settings script family); needs an EffectProbes entry + e2e test
setting script.launcher no integration-test exercise yet; burn-down #8474 (settings script family); needs an EffectProbes entry + e2e test
setting script.permissions no integration-test exercise yet; burn-down #8474 (settings script family); needs an EffectProbes entry + e2e test
setting script.profiles no integration-test exercise yet; burn-down #8474 (settings script family); needs an EffectProbes entry + e2e test
setting script.runtime no integration-test exercise yet; burn-down #8474 (settings script family); needs an EffectProbes entry + e2e test
setting script.runtimeOrder no integration-test exercise yet; burn-down #8474 (settings script family); needs an EffectProbes entry + e2e test
setting script.runtimes no integration-test exercise yet; burn-down #8474 (settings script family); needs an EffectProbes entry + e2e test
setting proto.elicitationPerMinute no integration-test exercise yet; burn-down #8473 (settings proto family); needs an EffectProbes entry + e2e test
setting proto.samplingPerMinute no integration-test exercise yet; burn-down #8473 (settings proto family); needs an EffectProbes entry + e2e test
setting proto.samplingSessionPerMinute no integration-test exercise yet; burn-down #8473 (settings proto family); needs an EffectProbes entry + e2e test
Expand Down
Loading