File size: 4,858 Bytes
96e86e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { randomUUID } from "node:crypto";
import { describe, expect, it } from "vitest";
import { pluginManifestV1Schema, type Issue } from "@paperclipai/shared";
import { createTestHarness } from "@paperclipai/plugin-sdk/testing";
import manifest from "../src/manifest.js";
import plugin from "../src/worker.js";

function issue(input: Partial<Issue> & Pick<Issue, "id" | "companyId" | "title">): Issue {
  const now = new Date();
  const { id, companyId, title, ...rest } = input;
  return {
    id,
    companyId,
    projectId: null,
    projectWorkspaceId: null,
    goalId: null,
    parentId: null,
    title,
    description: null,
    status: "todo",
    priority: "medium",
    assigneeAgentId: null,
    assigneeUserId: null,
    checkoutRunId: null,
    executionRunId: null,
    executionAgentNameKey: null,
    executionLockedAt: null,
    createdByAgentId: null,
    createdByUserId: null,
    issueNumber: null,
    identifier: null,
    originKind: "manual",
    originId: null,
    originRunId: null,
    requestDepth: 0,
    billingCode: null,
    assigneeAdapterOverrides: null,
    executionWorkspaceId: null,
    executionWorkspacePreference: null,
    executionWorkspaceSettings: null,
    startedAt: null,
    completedAt: null,
    cancelledAt: null,
    hiddenAt: null,
    createdAt: now,
    updatedAt: now,
    ...rest,
  };
}

describe("orchestration smoke plugin", () => {
  it("declares the Phase 1 orchestration surfaces", () => {
    expect(pluginManifestV1Schema.parse(manifest)).toMatchObject({
      id: "paperclipai.plugin-orchestration-smoke-example",
      database: {
        migrationsDir: "migrations",
        coreReadTables: ["issues"],
      },
      apiRoutes: [
        expect.objectContaining({ routeKey: "initialize" }),
        expect.objectContaining({ routeKey: "summary" }),
      ],
    });
  });

  it("creates plugin-owned orchestration rows, issue tree, document, wakeup, and summary reads", async () => {
    const companyId = randomUUID();
    const rootIssueId = randomUUID();
    const agentId = randomUUID();
    const harness = createTestHarness({ manifest });
    harness.seed({
      issues: [
        issue({
          id: rootIssueId,
          companyId,
          title: "Root orchestration issue",
          assigneeAgentId: agentId,
        }),
      ],
    });
    await plugin.definition.setup(harness.ctx);

    const result = await harness.performAction<{
      rootIssueId: string;
      childIssueId: string;
      blockerIssueId: string;
      billingCode: string;
      subtreeIssueIds: string[];
      wakeupQueued: boolean;
    }>("initialize-smoke", {
      companyId,
      issueId: rootIssueId,
      assigneeAgentId: agentId,
    });

    expect(result.rootIssueId).toBe(rootIssueId);
    expect(result.childIssueId).toEqual(expect.any(String));
    expect(result.blockerIssueId).toEqual(expect.any(String));
    expect(result.billingCode).toBe(`plugin-smoke:${rootIssueId}`);
    expect(result.wakeupQueued).toBe(true);
    expect(result.subtreeIssueIds).toEqual(expect.arrayContaining([rootIssueId, result.childIssueId]));
    expect(harness.dbExecutes[0]?.sql).toContain(".smoke_runs");
    expect(harness.dbQueries.some((entry) => entry.sql.includes("JOIN public.issues"))).toBe(true);

    const relations = await harness.ctx.issues.relations.get(result.childIssueId, companyId);
    expect(relations.blockedBy).toEqual([
      expect.objectContaining({
        id: result.blockerIssueId,
        status: "done",
      }),
    ]);
    const docs = await harness.ctx.issues.documents.list(result.childIssueId, companyId);
    expect(docs).toEqual([
      expect.objectContaining({
        key: "orchestration-smoke",
        title: "Orchestration Smoke",
      }),
    ]);
  });

  it("dispatches the scoped API route through the same smoke path", async () => {
    const companyId = randomUUID();
    const rootIssueId = randomUUID();
    const agentId = randomUUID();
    const harness = createTestHarness({ manifest });
    harness.seed({
      issues: [
        issue({
          id: rootIssueId,
          companyId,
          title: "Scoped API root",
          assigneeAgentId: agentId,
        }),
      ],
    });
    await plugin.definition.setup(harness.ctx);

    await expect(plugin.definition.onApiRequest?.({
      routeKey: "initialize",
      method: "POST",
      path: `/issues/${rootIssueId}/smoke`,
      params: { issueId: rootIssueId },
      query: {},
      body: { assigneeAgentId: agentId },
      actor: {
        actorType: "user",
        actorId: "board",
        userId: "board",
        agentId: null,
        runId: null,
      },
      companyId,
      headers: {},
    })).resolves.toMatchObject({
      status: 201,
      body: expect.objectContaining({
        rootIssueId,
        wakeupQueued: true,
      }),
    });
  });
});