mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-05 00:10:25 +00:00
test: isolate runtime state in memory tests
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { withTempDir } from "../test-helpers/temp-dir.js";
|
||||
import {
|
||||
findLatestTaskForRelatedSessionKeyForOwner,
|
||||
findTaskByRunIdForOwner,
|
||||
@@ -7,108 +8,132 @@ import {
|
||||
} from "./task-owner-access.js";
|
||||
import { createTaskRecord, resetTaskRegistryForTests } from "./task-registry.js";
|
||||
|
||||
const ORIGINAL_STATE_DIR = process.env.OPENCLAW_STATE_DIR;
|
||||
|
||||
afterEach(() => {
|
||||
resetTaskRegistryForTests({ persist: false });
|
||||
if (ORIGINAL_STATE_DIR == null) {
|
||||
delete process.env.OPENCLAW_STATE_DIR;
|
||||
} else {
|
||||
process.env.OPENCLAW_STATE_DIR = ORIGINAL_STATE_DIR;
|
||||
}
|
||||
});
|
||||
|
||||
async function withTaskRegistryTempDir<T>(run: () => Promise<T> | T): Promise<T> {
|
||||
return await withTempDir({ prefix: "openclaw-task-owner-access-" }, async (root) => {
|
||||
process.env.OPENCLAW_STATE_DIR = root;
|
||||
resetTaskRegistryForTests({ persist: false });
|
||||
return await run();
|
||||
});
|
||||
}
|
||||
|
||||
describe("task owner access", () => {
|
||||
it("returns owner-scoped tasks for owner and child-session lookups", () => {
|
||||
const task = createTaskRecord({
|
||||
runtime: "subagent",
|
||||
ownerKey: "agent:main:main",
|
||||
scopeKind: "session",
|
||||
childSessionKey: "agent:main:subagent:child-1",
|
||||
runId: "owner-visible-run",
|
||||
task: "Owner visible task",
|
||||
status: "running",
|
||||
});
|
||||
|
||||
expect(
|
||||
findLatestTaskForRelatedSessionKeyForOwner({
|
||||
relatedSessionKey: "agent:main:subagent:child-1",
|
||||
callerOwnerKey: "agent:main:main",
|
||||
})?.taskId,
|
||||
).toBe(task.taskId);
|
||||
expect(
|
||||
findTaskByRunIdForOwner({
|
||||
it("returns owner-scoped tasks for owner and child-session lookups", async () => {
|
||||
await withTaskRegistryTempDir(() => {
|
||||
const task = createTaskRecord({
|
||||
runtime: "subagent",
|
||||
ownerKey: "agent:main:main",
|
||||
scopeKind: "session",
|
||||
childSessionKey: "agent:main:subagent:child-1",
|
||||
runId: "owner-visible-run",
|
||||
callerOwnerKey: "agent:main:main",
|
||||
})?.taskId,
|
||||
).toBe(task.taskId);
|
||||
task: "Owner visible task",
|
||||
status: "running",
|
||||
});
|
||||
|
||||
expect(
|
||||
findLatestTaskForRelatedSessionKeyForOwner({
|
||||
relatedSessionKey: "agent:main:subagent:child-1",
|
||||
callerOwnerKey: "agent:main:main",
|
||||
})?.taskId,
|
||||
).toBe(task.taskId);
|
||||
expect(
|
||||
findTaskByRunIdForOwner({
|
||||
runId: "owner-visible-run",
|
||||
callerOwnerKey: "agent:main:main",
|
||||
})?.taskId,
|
||||
).toBe(task.taskId);
|
||||
});
|
||||
});
|
||||
|
||||
it("denies cross-owner task reads", () => {
|
||||
const task = createTaskRecord({
|
||||
runtime: "acp",
|
||||
ownerKey: "agent:main:main",
|
||||
scopeKind: "session",
|
||||
childSessionKey: "agent:main:acp:child-1",
|
||||
runId: "owner-hidden-run",
|
||||
task: "Hidden task",
|
||||
status: "queued",
|
||||
});
|
||||
|
||||
expect(
|
||||
getTaskByIdForOwner({
|
||||
taskId: task.taskId,
|
||||
callerOwnerKey: "agent:main:subagent:other-parent",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
findTaskByRunIdForOwner({
|
||||
it("denies cross-owner task reads", async () => {
|
||||
await withTaskRegistryTempDir(() => {
|
||||
const task = createTaskRecord({
|
||||
runtime: "acp",
|
||||
ownerKey: "agent:main:main",
|
||||
scopeKind: "session",
|
||||
childSessionKey: "agent:main:acp:child-1",
|
||||
runId: "owner-hidden-run",
|
||||
callerOwnerKey: "agent:main:subagent:other-parent",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
resolveTaskForLookupTokenForOwner({
|
||||
token: "agent:main:acp:child-1",
|
||||
callerOwnerKey: "agent:main:subagent:other-parent",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
task: "Hidden task",
|
||||
status: "queued",
|
||||
});
|
||||
|
||||
expect(
|
||||
getTaskByIdForOwner({
|
||||
taskId: task.taskId,
|
||||
callerOwnerKey: "agent:main:subagent:other-parent",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
findTaskByRunIdForOwner({
|
||||
runId: "owner-hidden-run",
|
||||
callerOwnerKey: "agent:main:subagent:other-parent",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
resolveTaskForLookupTokenForOwner({
|
||||
token: "agent:main:acp:child-1",
|
||||
callerOwnerKey: "agent:main:subagent:other-parent",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
it("requires an exact owner-key match", () => {
|
||||
const task = createTaskRecord({
|
||||
runtime: "acp",
|
||||
ownerKey: "agent:main:MixedCase",
|
||||
scopeKind: "session",
|
||||
runId: "case-sensitive-owner-run",
|
||||
task: "Case-sensitive owner",
|
||||
status: "queued",
|
||||
});
|
||||
it("requires an exact owner-key match", async () => {
|
||||
await withTaskRegistryTempDir(() => {
|
||||
const task = createTaskRecord({
|
||||
runtime: "acp",
|
||||
ownerKey: "agent:main:MixedCase",
|
||||
scopeKind: "session",
|
||||
runId: "case-sensitive-owner-run",
|
||||
task: "Case-sensitive owner",
|
||||
status: "queued",
|
||||
});
|
||||
|
||||
expect(
|
||||
getTaskByIdForOwner({
|
||||
taskId: task.taskId,
|
||||
callerOwnerKey: "agent:main:mixedcase",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
getTaskByIdForOwner({
|
||||
taskId: task.taskId,
|
||||
callerOwnerKey: "agent:main:mixedcase",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
it("does not expose system-owned tasks through owner-scoped readers", () => {
|
||||
const task = createTaskRecord({
|
||||
runtime: "cron",
|
||||
ownerKey: "system:cron:nightly",
|
||||
scopeKind: "system",
|
||||
childSessionKey: "agent:main:cron:nightly",
|
||||
runId: "system-task-run",
|
||||
task: "Nightly cron",
|
||||
status: "running",
|
||||
deliveryStatus: "not_applicable",
|
||||
});
|
||||
it("does not expose system-owned tasks through owner-scoped readers", async () => {
|
||||
await withTaskRegistryTempDir(() => {
|
||||
const task = createTaskRecord({
|
||||
runtime: "cron",
|
||||
ownerKey: "system:cron:nightly",
|
||||
scopeKind: "system",
|
||||
requesterSessionKey: "system:cron:nightly",
|
||||
childSessionKey: "agent:main:cron:nightly",
|
||||
runId: "system-task-run",
|
||||
task: "Nightly cron",
|
||||
status: "running",
|
||||
deliveryStatus: "not_applicable",
|
||||
});
|
||||
|
||||
expect(
|
||||
getTaskByIdForOwner({
|
||||
taskId: task.taskId,
|
||||
callerOwnerKey: "agent:main:main",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
resolveTaskForLookupTokenForOwner({
|
||||
token: "system-task-run",
|
||||
callerOwnerKey: "agent:main:main",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
getTaskByIdForOwner({
|
||||
taskId: task.taskId,
|
||||
callerOwnerKey: "agent:main:main",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
resolveTaskForLookupTokenForOwner({
|
||||
token: "system-task-run",
|
||||
callerOwnerKey: "agent:main:main",
|
||||
}),
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user