源码快照 @ 2026-09-02 (source-only, 剔除非源码素材)
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
import { main, parseWorldArgs } from "../src/cli.ts";
|
||||
import { discoverWorlds, resolveWorldScript } from "../src/loader.ts";
|
||||
|
||||
test("world arguments expose only script lifecycle flags and forward arguments after --", () => {
|
||||
assert.deepEqual(
|
||||
parseWorldArgs([
|
||||
"up",
|
||||
"./worlds/dev-headless.ts",
|
||||
"--detach",
|
||||
"--timeout",
|
||||
"5000",
|
||||
"--",
|
||||
"--replace",
|
||||
"value",
|
||||
]),
|
||||
{
|
||||
kind: "up",
|
||||
source: "./worlds/dev-headless.ts",
|
||||
detach: true,
|
||||
timeoutMs: 5000,
|
||||
args: ["--replace", "value"],
|
||||
},
|
||||
);
|
||||
|
||||
const foregroundTimeout = parseWorldArgs(["up", "dev-headless", "--timeout", "5000"]);
|
||||
assert.equal(foregroundTimeout.kind, "help");
|
||||
if (foregroundTimeout.kind !== "help") throw new Error("expected help");
|
||||
assert.match(foregroundTimeout.error ?? "", /only with --detach/);
|
||||
|
||||
const oldFlag = parseWorldArgs(["up", "dev-headless", "--keep"]);
|
||||
assert.equal(oldFlag.kind, "help");
|
||||
if (oldFlag.kind !== "help") throw new Error("expected help");
|
||||
assert.match(oldFlag.error ?? "", /Unknown world CLI option "--keep"/);
|
||||
|
||||
const oldCommand = parseWorldArgs(["resume", "dev-headless"]);
|
||||
assert.equal(oldCommand.kind, "help");
|
||||
if (oldCommand.kind !== "help") throw new Error("expected help");
|
||||
assert.match(oldCommand.error ?? "", /Unknown command "resume"/);
|
||||
});
|
||||
|
||||
test("discovery, resolution, list, and help classify scripts without importing them", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "openwork-world-cli-discovery-"));
|
||||
try {
|
||||
const worldsDirectory = join(root, "worlds");
|
||||
const fixturePath = join(worldsDirectory, "throwing.ts");
|
||||
await mkdir(worldsDirectory);
|
||||
await writeFile(fixturePath, 'throw new Error("must not import");\n', "utf8");
|
||||
|
||||
assert.deepEqual(await discoverWorlds(worldsDirectory), [{
|
||||
kind: "script",
|
||||
name: "throwing",
|
||||
path: fixturePath,
|
||||
}]);
|
||||
for (const source of ["throwing", "throwing.ts", fixturePath]) {
|
||||
assert.deepEqual(await resolveWorldScript(source, { cwd: root, worldsDirectory }), {
|
||||
kind: "script",
|
||||
name: "throwing",
|
||||
path: fixturePath,
|
||||
});
|
||||
}
|
||||
|
||||
for (const command of [["list"], ["help"]]) {
|
||||
const lines: string[] = [];
|
||||
assert.equal(await main(command, {
|
||||
cwd: root,
|
||||
worldsDirectory,
|
||||
print: (line) => lines.push(line),
|
||||
}), 0);
|
||||
assert.match(lines.join("\n"), /world scripts/i);
|
||||
assert.match(lines.join("\n"), /throwing/);
|
||||
}
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("foreground scripts receive argv after -- and mirror their exit code", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "openwork-world-cli-argv-"));
|
||||
try {
|
||||
const worldsDirectory = join(root, "worlds");
|
||||
const outputPath = join(root, "argv.json");
|
||||
await mkdir(worldsDirectory);
|
||||
await writeFile(join(worldsDirectory, "argv.ts"), `
|
||||
import { writeFile } from "node:fs/promises";
|
||||
await writeFile(process.argv[2], JSON.stringify(process.argv.slice(3)), "utf8");
|
||||
process.exitCode = 7;
|
||||
`, "utf8");
|
||||
|
||||
const code = await main(["up", "argv", "--", outputPath, "--detach", "plain"], {
|
||||
cwd: root,
|
||||
worldsDirectory,
|
||||
print: () => {},
|
||||
});
|
||||
assert.equal(code, 7);
|
||||
const recordedArgs: unknown = JSON.parse(await readFile(outputPath, "utf8"));
|
||||
assert.deepEqual(recordedArgs, ["--detach", "plain"]);
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,137 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import test from "node:test";
|
||||
import {
|
||||
assertHeadlessLaunchSafety,
|
||||
installedProductionHeadlessEnv,
|
||||
resolveHeadlessClientConnection,
|
||||
resolveInstalledProductionHeadlessState,
|
||||
resolveHeadlessWorldRuntimePaths,
|
||||
} from "../src/headless-web.ts";
|
||||
|
||||
test("headless production state resolves installed stores and credentials without copying them", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "openwork-headless-production-"));
|
||||
try {
|
||||
const dataDir = join(root, ".openwork", "openwork-server");
|
||||
const configDir = join(root, ".config", "openwork");
|
||||
const opencodeConfigDir = join(root, ".config", "opencode");
|
||||
const userDataDir = join(root, "Library", "Application Support", "com.differentai.openwork");
|
||||
const opencodeDb = join(root, "Library", "Application Support", "opencode", "opencode.db");
|
||||
await mkdir(dataDir, { recursive: true });
|
||||
await mkdir(configDir, { recursive: true });
|
||||
await mkdir(opencodeConfigDir, { recursive: true });
|
||||
await mkdir(userDataDir, { recursive: true });
|
||||
await mkdir(dirname(opencodeDb), { recursive: true });
|
||||
await writeFile(opencodeDb, "fixture", "utf8");
|
||||
for (const name of ["desktop-bootstrap.json", "env.json", "server.json"]) {
|
||||
await writeFile(join(configDir, name), "{}\n", "utf8");
|
||||
}
|
||||
await writeFile(join(userDataDir, "openwork-server-state.json"), "{}\n", "utf8");
|
||||
await writeFile(join(userDataDir, "openwork-workspaces.json"), JSON.stringify({
|
||||
workspaces: [{ path: "/Users/me/production-workspace" }],
|
||||
}), "utf8");
|
||||
await writeFile(join(userDataDir, "openwork-server-tokens.json"), JSON.stringify({
|
||||
version: 2,
|
||||
credentials: { clientToken: "client-token", hostToken: "host-token" },
|
||||
}), "utf8");
|
||||
|
||||
const state = await resolveInstalledProductionHeadlessState({
|
||||
env: {},
|
||||
fallbackWorkspace: "/fallback",
|
||||
homeDir: root,
|
||||
platform: "darwin",
|
||||
});
|
||||
assert.equal(state.workspace, "/Users/me/production-workspace");
|
||||
assert.equal(state.token, "client-token");
|
||||
assert.equal(state.hostToken, "host-token");
|
||||
assert.equal(state.opencodeDb, opencodeDb);
|
||||
const env = installedProductionHeadlessEnv(state);
|
||||
assert.equal(env.OPENWORK_DATA_DIR, dataDir);
|
||||
assert.equal(env.OPENWORK_SERVER_CONFIG, join(configDir, "server.json"));
|
||||
assert.equal(env.OPENCODE_DB, opencodeDb);
|
||||
assert.equal(env.OPENWORK_DEV_SHARED_STATE, "1");
|
||||
assert.equal(env.OPENWORK_DESKTOP_DISABLE_WORKSPACE_RECOVERY, "0");
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("headless production state remains macOS-only", async () => {
|
||||
await assert.rejects(
|
||||
() => resolveInstalledProductionHeadlessState({
|
||||
env: {},
|
||||
fallbackWorkspace: "/tmp",
|
||||
homeDir: "/tmp",
|
||||
platform: "linux",
|
||||
}),
|
||||
/supported only on macOS/,
|
||||
);
|
||||
});
|
||||
|
||||
test("headless production state refuses every remote exposure setting", () => {
|
||||
for (const env of [
|
||||
{ OPENWORK_REMOTE_ACCESS: "1" },
|
||||
{ OPENWORK_PUBLIC_HOST: "dev.example.com" },
|
||||
{ HOST: "0.0.0.0" },
|
||||
{ VITE_HOST: "192.0.2.10" },
|
||||
]) {
|
||||
assert.throws(
|
||||
() => assertHeadlessLaunchSafety("installed-production", env),
|
||||
/require loopback-only access/,
|
||||
);
|
||||
}
|
||||
assert.doesNotThrow(() => assertHeadlessLaunchSafety("installed-production", {
|
||||
HOST: "127.0.0.1",
|
||||
VITE_HOST: "localhost",
|
||||
}));
|
||||
assert.doesNotThrow(() => assertHeadlessLaunchSafety("isolated", {
|
||||
OPENWORK_REMOTE_ACCESS: "1",
|
||||
}));
|
||||
});
|
||||
|
||||
test("headless production state pins browser credentials to its generated loopback server", () => {
|
||||
const production = resolveHeadlessClientConnection({
|
||||
state: "installed-production",
|
||||
env: {
|
||||
VITE_OPENWORK_URL: "https://attacker.example",
|
||||
VITE_OPENWORK_PORT: "443",
|
||||
VITE_OPENWORK_TOKEN: "attacker-selected-token",
|
||||
},
|
||||
openworkUrl: "http://127.0.0.1:8778",
|
||||
openworkPort: 8778,
|
||||
token: "installed-production-token",
|
||||
});
|
||||
assert.deepEqual(production, {
|
||||
url: "http://127.0.0.1:8778",
|
||||
port: "8778",
|
||||
token: "installed-production-token",
|
||||
});
|
||||
|
||||
const isolated = resolveHeadlessClientConnection({
|
||||
state: "isolated",
|
||||
env: { VITE_OPENWORK_URL: "http://127.0.0.1:9999" },
|
||||
openworkUrl: "http://127.0.0.1:8778",
|
||||
openworkPort: 8778,
|
||||
token: "isolated-token",
|
||||
});
|
||||
assert.equal(isolated.url, "http://127.0.0.1:9999");
|
||||
});
|
||||
|
||||
test("named headless worlds own separate runtime paths while the compatibility world keeps legacy paths", () => {
|
||||
const repoRoot = "/repo/openwork";
|
||||
const compatibility = resolveHeadlessWorldRuntimePaths(repoRoot, "dev-headless");
|
||||
const production = resolveHeadlessWorldRuntimePaths(repoRoot, "headless-prod-live");
|
||||
|
||||
assert.equal(compatibility.runtimeManifestPath, join(repoRoot, "tmp", "dev-headless-web.json"));
|
||||
assert.equal(production.runtimeManifestPath, join(
|
||||
repoRoot,
|
||||
"tmp",
|
||||
"worlds",
|
||||
"runtime",
|
||||
"headless-prod-live",
|
||||
"runtime.json",
|
||||
));
|
||||
assert.notEqual(production.serverConfigPath, compatibility.serverConfigPath);
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtemp, rm, stat } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
import { WorldStateStore } from "../src/store.ts";
|
||||
|
||||
test("local world state is owner-only and addressable by world name", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "openwork-world-store-"));
|
||||
try {
|
||||
const store = new WorldStateStore(join(root, "worlds"));
|
||||
const path = await store.save("demo", '{"name":"demo"}');
|
||||
assert.equal((await stat(path)).mode & 0o777, 0o600);
|
||||
assert.equal(await store.read("demo"), '{"name":"demo"}\n');
|
||||
assert.deepEqual(await store.list(), [path]);
|
||||
assert.equal(await store.forget("demo"), true);
|
||||
assert.equal(await store.forget("demo"), false);
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user