taskIndexFixture.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import type { ZCodeTaskMeta } from "@zcode/shared";
  2. import type { TaskIndexRepo } from "#src/session/taskIndexRepo.js";
  3. /** 测试分别设置 CLI 事实和 Host 组织态,不为夹具保留生产环境的整行写入入口。 */
  4. export async function seedTaskIndex(
  5. repo: TaskIndexRepo,
  6. input: {
  7. meta: ZCodeTaskMeta;
  8. pinned?: boolean;
  9. archived?: boolean;
  10. deleted?: boolean;
  11. titleOverridden?: boolean;
  12. searchableText?: string;
  13. },
  14. ): Promise<ZCodeTaskMeta> {
  15. const { meta } = input;
  16. await repo.applyCliFacts({
  17. meta,
  18. titleSource: (input.titleOverridden ?? meta.titleOverridden) ? "custom" : "generated",
  19. lastOutcome: meta.status === "completed" || meta.status === "error" ? meta.status : undefined,
  20. });
  21. const {
  22. mode,
  23. model,
  24. thoughtLevel,
  25. lastError,
  26. target,
  27. migrationSource,
  28. cronAutomationId,
  29. offPeakTaskId,
  30. } = meta;
  31. await repo.updateTaskPresentation({
  32. ...meta,
  33. presentation: Object.fromEntries(
  34. Object.entries({
  35. mode,
  36. model,
  37. thoughtLevel,
  38. lastError,
  39. target,
  40. migrationSource,
  41. cronAutomationId,
  42. offPeakTaskId,
  43. }).filter(([, value]) => value !== undefined),
  44. ),
  45. searchableText: input.searchableText,
  46. });
  47. if (input.pinned !== undefined || input.archived !== undefined)
  48. await repo.updateTaskState({
  49. ...meta,
  50. patch: {
  51. ...(input.pinned !== undefined ? { pinned: input.pinned } : {}),
  52. ...(input.archived !== undefined ? { archived: input.archived } : {}),
  53. },
  54. });
  55. if (input.deleted) await repo.applyCliDeletion(meta);
  56. return (await repo.getTaskMeta(meta))!;
  57. }