sshRemoteP0Replay.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { mkdtemp, rm } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { fileURLToPath } from "node:url";
  5. import { expect, it } from "vitest";
  6. import { startUpstreamReplayServer } from "#e2e/helpers/upstream-replay-server";
  7. it.each(["a-first", "b-first", "a-follow", "b-follow"])(
  8. "SSH P0 %s 先输出正文,显式释放后才结束回合",
  9. async (turn) => {
  10. const dir = await mkdtemp(join(tmpdir(), "ssh-p0-replay-"));
  11. const server = await startUpstreamReplayServer({
  12. artifactPath: join(dir, "capture.json"),
  13. fixturePaths: [
  14. fileURLToPath(
  15. new URL(
  16. "./e2e/fixtures/upstream/conversation-session/conversation-session-ssh-remote-p0.json",
  17. import.meta.url,
  18. ),
  19. ),
  20. ],
  21. });
  22. try {
  23. const [label, phase] = turn.toUpperCase().split("-");
  24. const messages = [
  25. ...(phase === "FOLLOW"
  26. ? [{ role: "user", content: `E2E_SSH_REMOTE_P0_${label}_FIRST_PROMPT` }]
  27. : []),
  28. { role: "user", content: `E2E_SSH_REMOTE_P0_${label}_${phase}_PROMPT` },
  29. ];
  30. const response = await fetch(`${server.baseUrl}/chat/completions`, {
  31. method: "POST",
  32. headers: { "content-type": "application/json" },
  33. body: JSON.stringify({ messages }),
  34. signal: AbortSignal.timeout(5000),
  35. });
  36. expect(response.status).toBe(200);
  37. const reader = response.body!.getReader();
  38. const first = new TextDecoder().decode((await reader.read()).value);
  39. expect(first).toContain(`E2E_SSH_REMOTE_P0_${label}_${phase}_REPLY`);
  40. expect(first).not.toContain("[DONE]");
  41. const fixtureId = `ssh-remote-p0-${turn}`;
  42. expect(await fetch(`${server.baseUrl}/__e2e__/gates`).then((value) => value.json())).toEqual([
  43. fixtureId,
  44. ]);
  45. expect(
  46. (await fetch(`${server.baseUrl}/__e2e__/release/${fixtureId}`, { method: "POST" })).status,
  47. ).toBe(204);
  48. let rest = "";
  49. for (;;) {
  50. const chunk = await reader.read();
  51. if (chunk.done) break;
  52. rest += new TextDecoder().decode(chunk.value);
  53. }
  54. expect(rest).toContain("[DONE]");
  55. } finally {
  56. await server.stop();
  57. await rm(dir, { recursive: true, force: true });
  58. }
  59. },
  60. );