session-test-utils.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { existsSync, mkdirSync, rmSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import type { AgentMessage } from "@earendil-works/pi-agent-core";
  5. import { afterEach } from "vitest";
  6. export function createUserMessage(text: string): AgentMessage {
  7. return {
  8. role: "user",
  9. content: [{ type: "text", text }],
  10. timestamp: Date.now(),
  11. };
  12. }
  13. export function createAssistantMessage(text: string): AgentMessage {
  14. return {
  15. role: "assistant",
  16. content: [{ type: "text", text }],
  17. api: "anthropic-messages",
  18. provider: "anthropic",
  19. model: "claude-sonnet-4-5",
  20. usage: {
  21. input: 0,
  22. output: 0,
  23. cacheRead: 0,
  24. cacheWrite: 0,
  25. totalTokens: 0,
  26. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  27. },
  28. stopReason: "stop",
  29. timestamp: Date.now(),
  30. };
  31. }
  32. const tempDirs: string[] = [];
  33. export function createTempDir(): string {
  34. const dir = join(tmpdir(), `pi-agent-session-${Date.now()}-${Math.random().toString(36).slice(2)}`);
  35. mkdirSync(dir, { recursive: true });
  36. tempDirs.push(dir);
  37. return dir;
  38. }
  39. export function getLatestTempDir(): string {
  40. return tempDirs[tempDirs.length - 1]!;
  41. }
  42. afterEach(() => {
  43. while (tempDirs.length > 0) {
  44. const dir = tempDirs.pop()!;
  45. if (existsSync(dir)) {
  46. rmSync(dir, { recursive: true, force: true });
  47. }
  48. }
  49. });