Renamed test
This commit is contained in:
@@ -31,7 +31,7 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
process.chdir(tmpdir());
|
process.chdir(tmpdir());
|
||||||
});
|
});
|
||||||
|
|
||||||
async function createRuntimeHost(
|
async function createRuntimeForTest(
|
||||||
extensionFactory: ExtensionFactory,
|
extensionFactory: ExtensionFactory,
|
||||||
options?: { cwd?: string; bootstrapModel?: boolean; bootstrapThinkingLevel?: boolean },
|
options?: { cwd?: string; bootstrapModel?: boolean; bootstrapThinkingLevel?: boolean },
|
||||||
) {
|
) {
|
||||||
@@ -98,27 +98,27 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
diagnostics: services.diagnostics,
|
diagnostics: services.diagnostics,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
const runtimeHost = await createAgentSessionRuntime(createRuntime, {
|
const runtime = await createAgentSessionRuntime(createRuntime, {
|
||||||
cwd: tempDir,
|
cwd: tempDir,
|
||||||
agentDir: tempDir,
|
agentDir: tempDir,
|
||||||
sessionManager: SessionManager.create(tempDir),
|
sessionManager: SessionManager.create(tempDir),
|
||||||
});
|
});
|
||||||
await runtimeHost.session.bindExtensions({});
|
await runtime.session.bindExtensions({});
|
||||||
|
|
||||||
cleanups.push(async () => {
|
cleanups.push(async () => {
|
||||||
await runtimeHost.dispose();
|
await runtime.dispose();
|
||||||
faux.unregister();
|
faux.unregister();
|
||||||
if (existsSync(tempDir)) {
|
if (existsSync(tempDir)) {
|
||||||
rmSync(tempDir, { recursive: true, force: true });
|
rmSync(tempDir, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return { runtimeHost, faux, tempDir };
|
return { runtime, faux, tempDir };
|
||||||
}
|
}
|
||||||
|
|
||||||
it("emits session_before_switch and session_start for new and resume flows", async () => {
|
it("emits session_before_switch and session_start for new and resume flows", async () => {
|
||||||
const events: RecordedSessionEvent[] = [];
|
const events: RecordedSessionEvent[] = [];
|
||||||
const { runtimeHost } = await createRuntimeHost((pi: ExtensionAPI) => {
|
const { runtime } = await createRuntimeForTest((pi: ExtensionAPI) => {
|
||||||
pi.on("session_before_switch", (event) => {
|
pi.on("session_before_switch", (event) => {
|
||||||
events.push(event);
|
events.push(event);
|
||||||
});
|
});
|
||||||
@@ -130,26 +130,26 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
expect(events).toEqual([{ type: "session_start", reason: "startup" }]);
|
expect(events).toEqual([{ type: "session_start", reason: "startup" }]);
|
||||||
events.length = 0;
|
events.length = 0;
|
||||||
|
|
||||||
await runtimeHost.session.prompt("hello");
|
await runtime.session.prompt("hello");
|
||||||
const originalSessionFile = runtimeHost.session.sessionFile;
|
const originalSessionFile = runtime.session.sessionFile;
|
||||||
const originalSession = runtimeHost.session;
|
const originalSession = runtime.session;
|
||||||
|
|
||||||
const newSessionResult = await runtimeHost.newSession();
|
const newSessionResult = await runtime.newSession();
|
||||||
expect(newSessionResult.cancelled).toBe(false);
|
expect(newSessionResult.cancelled).toBe(false);
|
||||||
await runtimeHost.session.bindExtensions({});
|
await runtime.session.bindExtensions({});
|
||||||
expect(runtimeHost.session).not.toBe(originalSession);
|
expect(runtime.session).not.toBe(originalSession);
|
||||||
expect(runtimeHost.session.messages).toEqual([]);
|
expect(runtime.session.messages).toEqual([]);
|
||||||
expect(events).toEqual([
|
expect(events).toEqual([
|
||||||
{ type: "session_before_switch", reason: "new", targetSessionFile: undefined },
|
{ type: "session_before_switch", reason: "new", targetSessionFile: undefined },
|
||||||
{ type: "session_start", reason: "new", previousSessionFile: originalSessionFile },
|
{ type: "session_start", reason: "new", previousSessionFile: originalSessionFile },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
events.length = 0;
|
events.length = 0;
|
||||||
const secondSessionFile = runtimeHost.session.sessionFile;
|
const secondSessionFile = runtime.session.sessionFile;
|
||||||
|
|
||||||
const switchResult = await runtimeHost.switchSession(originalSessionFile!);
|
const switchResult = await runtime.switchSession(originalSessionFile!);
|
||||||
expect(switchResult.cancelled).toBe(false);
|
expect(switchResult.cancelled).toBe(false);
|
||||||
await runtimeHost.session.bindExtensions({});
|
await runtime.session.bindExtensions({});
|
||||||
expect(events).toEqual([
|
expect(events).toEqual([
|
||||||
{ type: "session_before_switch", reason: "resume", targetSessionFile: originalSessionFile },
|
{ type: "session_before_switch", reason: "resume", targetSessionFile: originalSessionFile },
|
||||||
{ type: "session_start", reason: "resume", previousSessionFile: secondSessionFile },
|
{ type: "session_start", reason: "resume", previousSessionFile: secondSessionFile },
|
||||||
@@ -159,7 +159,7 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
it("honors session_before_switch cancellation for new and resume", async () => {
|
it("honors session_before_switch cancellation for new and resume", async () => {
|
||||||
const events: RecordedSessionEvent[] = [];
|
const events: RecordedSessionEvent[] = [];
|
||||||
let cancelReason: "new" | "resume" | undefined;
|
let cancelReason: "new" | "resume" | undefined;
|
||||||
const { runtimeHost } = await createRuntimeHost((pi: ExtensionAPI) => {
|
const { runtime } = await createRuntimeForTest((pi: ExtensionAPI) => {
|
||||||
pi.on("session_before_switch", (event) => {
|
pi.on("session_before_switch", (event) => {
|
||||||
events.push(event);
|
events.push(event);
|
||||||
if (event.reason === cancelReason) {
|
if (event.reason === cancelReason) {
|
||||||
@@ -171,13 +171,13 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await runtimeHost.session.prompt("hello");
|
await runtime.session.prompt("hello");
|
||||||
const originalSessionFile = runtimeHost.session.sessionFile;
|
const originalSessionFile = runtime.session.sessionFile;
|
||||||
|
|
||||||
cancelReason = "new";
|
cancelReason = "new";
|
||||||
const newResult = await runtimeHost.newSession();
|
const newResult = await runtime.newSession();
|
||||||
expect(newResult.cancelled).toBe(true);
|
expect(newResult.cancelled).toBe(true);
|
||||||
expect(runtimeHost.session.sessionFile).toBe(originalSessionFile);
|
expect(runtime.session.sessionFile).toBe(originalSessionFile);
|
||||||
|
|
||||||
events.length = 0;
|
events.length = 0;
|
||||||
const otherDir = join(tmpdir(), `pi-runtime-other-${Date.now()}-${Math.random().toString(36).slice(2)}`);
|
const otherDir = join(tmpdir(), `pi-runtime-other-${Date.now()}-${Math.random().toString(36).slice(2)}`);
|
||||||
@@ -186,15 +186,15 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
otherSession.appendMessage({ role: "user", content: [{ type: "text", text: "other" }], timestamp: Date.now() });
|
otherSession.appendMessage({ role: "user", content: [{ type: "text", text: "other" }], timestamp: Date.now() });
|
||||||
const otherSessionFile = otherSession.getSessionFile();
|
const otherSessionFile = otherSession.getSessionFile();
|
||||||
cancelReason = "resume";
|
cancelReason = "resume";
|
||||||
const resumeResult = await runtimeHost.switchSession(otherSessionFile!);
|
const resumeResult = await runtime.switchSession(otherSessionFile!);
|
||||||
expect(resumeResult.cancelled).toBe(true);
|
expect(resumeResult.cancelled).toBe(true);
|
||||||
expect(runtimeHost.session.sessionFile).toBe(originalSessionFile);
|
expect(runtime.session.sessionFile).toBe(originalSessionFile);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("emits session_before_fork and session_start and honors cancellation", async () => {
|
it("emits session_before_fork and session_start and honors cancellation", async () => {
|
||||||
const events: RecordedSessionEvent[] = [];
|
const events: RecordedSessionEvent[] = [];
|
||||||
let cancelNextFork = false;
|
let cancelNextFork = false;
|
||||||
const { runtimeHost } = await createRuntimeHost((pi: ExtensionAPI) => {
|
const { runtime } = await createRuntimeForTest((pi: ExtensionAPI) => {
|
||||||
pi.on("session_before_fork", (event) => {
|
pi.on("session_before_fork", (event) => {
|
||||||
events.push(event);
|
events.push(event);
|
||||||
if (cancelNextFork) {
|
if (cancelNextFork) {
|
||||||
@@ -208,14 +208,14 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
events.length = 0;
|
events.length = 0;
|
||||||
await runtimeHost.session.prompt("hello");
|
await runtime.session.prompt("hello");
|
||||||
const userMessage = runtimeHost.session.getUserMessagesForForking()[0]!;
|
const userMessage = runtime.session.getUserMessagesForForking()[0]!;
|
||||||
const previousSessionFile = runtimeHost.session.sessionFile;
|
const previousSessionFile = runtime.session.sessionFile;
|
||||||
|
|
||||||
const successResult = await runtimeHost.fork(userMessage.entryId);
|
const successResult = await runtime.fork(userMessage.entryId);
|
||||||
expect(successResult.cancelled).toBe(false);
|
expect(successResult.cancelled).toBe(false);
|
||||||
expect(successResult.selectedText).toBe("hello");
|
expect(successResult.selectedText).toBe("hello");
|
||||||
await runtimeHost.session.bindExtensions({});
|
await runtime.session.bindExtensions({});
|
||||||
expect(events).toEqual([
|
expect(events).toEqual([
|
||||||
{ type: "session_before_fork", entryId: userMessage.entryId },
|
{ type: "session_before_fork", entryId: userMessage.entryId },
|
||||||
{ type: "session_start", reason: "fork", previousSessionFile },
|
{ type: "session_start", reason: "fork", previousSessionFile },
|
||||||
@@ -223,14 +223,14 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
|
|
||||||
events.length = 0;
|
events.length = 0;
|
||||||
cancelNextFork = true;
|
cancelNextFork = true;
|
||||||
const cancelResult = await runtimeHost.fork(userMessage.entryId);
|
const cancelResult = await runtime.fork(userMessage.entryId);
|
||||||
expect(cancelResult).toEqual({ cancelled: true });
|
expect(cancelResult).toEqual({ cancelled: true });
|
||||||
expect(events).toEqual([{ type: "session_before_fork", entryId: userMessage.entryId }]);
|
expect(events).toEqual([{ type: "session_before_fork", entryId: userMessage.entryId }]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws when forking with an invalid entry id", async () => {
|
it("throws when forking with an invalid entry id", async () => {
|
||||||
const { runtimeHost } = await createRuntimeHost(() => {});
|
const { runtime } = await createRuntimeForTest(() => {});
|
||||||
await expect(runtimeHost.fork("missing-entry")).rejects.toThrow("Invalid entry ID for forking");
|
await expect(runtime.fork("missing-entry")).rejects.toThrow("Invalid entry ID for forking");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("updates process.cwd() on cross-cwd session replacement", async () => {
|
it("updates process.cwd() on cross-cwd session replacement", async () => {
|
||||||
@@ -238,7 +238,7 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
const secondDir = join(tmpdir(), `pi-runtime-cwd-b-${Date.now()}-${Math.random().toString(36).slice(2)}`);
|
const secondDir = join(tmpdir(), `pi-runtime-cwd-b-${Date.now()}-${Math.random().toString(36).slice(2)}`);
|
||||||
mkdirSync(firstDir, { recursive: true });
|
mkdirSync(firstDir, { recursive: true });
|
||||||
mkdirSync(secondDir, { recursive: true });
|
mkdirSync(secondDir, { recursive: true });
|
||||||
const { runtimeHost, faux, tempDir } = await createRuntimeHost(() => {}, { cwd: firstDir });
|
const { runtime, faux, tempDir } = await createRuntimeForTest(() => {}, { cwd: firstDir });
|
||||||
const otherAuthStorage = AuthStorage.inMemory();
|
const otherAuthStorage = AuthStorage.inMemory();
|
||||||
otherAuthStorage.setRuntimeApiKey(faux.getModel().provider, "faux-key");
|
otherAuthStorage.setRuntimeApiKey(faux.getModel().provider, "faux-key");
|
||||||
const otherRuntimeOptions = {
|
const otherRuntimeOptions = {
|
||||||
@@ -299,14 +299,14 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
await otherRuntime.session.prompt("other");
|
await otherRuntime.session.prompt("other");
|
||||||
const otherSessionFile = otherRuntime.session.sessionFile!;
|
const otherSessionFile = otherRuntime.session.sessionFile!;
|
||||||
|
|
||||||
await runtimeHost.switchSession(otherSessionFile);
|
await runtime.switchSession(otherSessionFile);
|
||||||
|
|
||||||
expect(realpathSync(process.cwd())).toBe(realpathSync(secondDir));
|
expect(realpathSync(process.cwd())).toBe(realpathSync(secondDir));
|
||||||
expect(realpathSync(runtimeHost.session.sessionManager.getCwd())).toBe(realpathSync(secondDir));
|
expect(realpathSync(runtime.session.sessionManager.getCwd())).toBe(realpathSync(secondDir));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("restores model and thinking state from the destination session", async () => {
|
it("restores model and thinking state from the destination session", async () => {
|
||||||
const { runtimeHost, faux, tempDir } = await createRuntimeHost(() => {}, {
|
const { runtime, faux, tempDir } = await createRuntimeForTest(() => {}, {
|
||||||
bootstrapModel: false,
|
bootstrapModel: false,
|
||||||
bootstrapThinkingLevel: false,
|
bootstrapThinkingLevel: false,
|
||||||
});
|
});
|
||||||
@@ -374,9 +374,9 @@ describe("AgentSessionRuntime characterization", () => {
|
|||||||
await otherRuntime.session.prompt("hello");
|
await otherRuntime.session.prompt("hello");
|
||||||
const targetSessionFile = otherRuntime.session.sessionFile!;
|
const targetSessionFile = otherRuntime.session.sessionFile!;
|
||||||
|
|
||||||
await runtimeHost.switchSession(targetSessionFile);
|
await runtime.switchSession(targetSessionFile);
|
||||||
|
|
||||||
expect(runtimeHost.session.model?.id).toBe("faux-2");
|
expect(runtime.session.model?.id).toBe("faux-2");
|
||||||
expect(runtimeHost.session.thinkingLevel).toBe("off");
|
expect(runtime.session.thinkingLevel).toBe("off");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user