model-config-mutation.test.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeApp } from "../src/app/types.js";
  3. import { runSessionModelConfigMutation } from "../src/zcode-protocol-v4/model-config-mutation.js";
  4. describe("session model config mutation coordinator", () => {
  5. it("serializes registry fallback and explicit model mutations by session App identity", async () => {
  6. const app = {} as ZCodeApp;
  7. const order: string[] = [];
  8. let releaseFirst!: () => void;
  9. const firstBlocked = new Promise<void>((resolve) => {
  10. releaseFirst = resolve;
  11. });
  12. const first = runSessionModelConfigMutation(app, async () => {
  13. order.push("fallback:start");
  14. await firstBlocked;
  15. order.push("fallback:end");
  16. });
  17. const second = runSessionModelConfigMutation(app, async () => {
  18. order.push("explicit:start");
  19. order.push("explicit:end");
  20. });
  21. await new Promise<void>((resolve) => {
  22. queueMicrotask(() => queueMicrotask(resolve));
  23. });
  24. expect(order).toEqual(["fallback:start"]);
  25. releaseFirst();
  26. await Promise.all([first, second]);
  27. expect(order).toEqual(["fallback:start", "fallback:end", "explicit:start", "explicit:end"]);
  28. });
  29. it("does not poison later model mutations when one operation fails", async () => {
  30. const app = {} as ZCodeApp;
  31. const failed = runSessionModelConfigMutation(app, async () => {
  32. throw new Error("expected failure");
  33. });
  34. const recovered = runSessionModelConfigMutation(app, async () => "recovered");
  35. await expect(failed).rejects.toThrow("expected failure");
  36. await expect(recovered).resolves.toBe("recovered");
  37. });
  38. });