File size: 6,497 Bytes
837e3ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import http from "node:http";
import { readFile } from "node:fs/promises";
import path from "node:path";

const appBaseUrl = process.env.APP_BASE_URL || "http://127.0.0.1:3000";
const seedPath = path.join(
  process.cwd(),
  "agentic_pm_demo_codex_plans/data/work-packages.seed.json",
);

function assert(condition, message) {
  if (!condition) {
    throw new Error(message);
  }
}

function buildFakeLlmResponse(joinedMessages) {
  if (joinedMessages.includes("Connection test.")) {
    return "Connected to the configured model.";
  }

  if (joinedMessages.includes('"mode": "ask"')) {
    return "Verification method explains how the requirement will be checked, such as by test, inspection, or analysis.";
  }

  if (joinedMessages.includes('"mode": "plan"')) {
    return "Please break this package into review tasks.";
  }

  if (joinedMessages.includes('"mode": "change"')) {
    return "Please update the objective to include cybersecurity acceptance criteria.";
  }

  return JSON.stringify({
    choices: [
      {
        message: {
          content: JSON.stringify({
            assistantMessage: "Fallback JSON response.",
            boardAction: { type: "none", workPackageId: null },
          }),
        },
      },
    ],
  });
}

async function startFakeLlmServer() {
  const server = http.createServer((req, res) => {
    let body = "";
    req.on("data", (chunk) => {
      body += chunk;
    });
    req.on("end", () => {
      let payload = {};
      try {
        payload = JSON.parse(body || "{}");
      } catch {
        payload = {};
      }

      const joinedMessages = Array.isArray(payload.messages)
        ? payload.messages.map((message) => String(message?.content || "")).join("\n\n")
        : "";

      res.writeHead(200, { "content-type": "text/plain" });
      res.end(buildFakeLlmResponse(joinedMessages));
    });
  });

  await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
  const address = server.address();
  if (!address || typeof address === "string") {
    throw new Error("Could not determine fake LLM server address.");
  }

  return {
    server,
    baseUrl: `http://127.0.0.1:${address.port}/v1`,
  };
}

async function postJson(url, body) {
  const response = await fetch(url, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify(body),
  });

  const payload = await response.json();
  if (!response.ok) {
    throw new Error(`Request failed (${response.status}): ${JSON.stringify(payload)}`);
  }

  return payload;
}

async function main() {
  const seed = JSON.parse(await readFile(seedPath, "utf8"));
  const { server, baseUrl } = await startFakeLlmServer();

  try {
    console.log(`Smoke target: ${appBaseUrl}`);
    console.log(`Fake LLM: ${baseUrl}`);

    const llmConfig = {
      apiKey: "live-key",
      baseUrl,
      model: "fake-model",
    };

    const connection = await postJson(`${appBaseUrl}/api/test-connection`, {
      llmConfig,
    });
    assert(connection.ok === true, "Connection test did not succeed.");
    assert(
      connection.message === "Connected to the configured model.",
      "Connection test message mismatch.",
    );
    console.log("PASS connection");

    const ask = await postJson(`${appBaseUrl}/api/chat`, {
      messages: [{ role: "user", content: "@SRS ask What does verification method mean?" }],
      workPackages: seed,
      selectedWorkPackageId: "wp-srs",
      parsedCommand: {
        referencedPackageName: "System Requirements Specification",
        mode: "ask",
        instruction: "What does verification method mean?",
      },
      llmConfig,
    });
    assert(
      typeof ask.assistantMessage === "string" &&
        ask.assistantMessage.includes("Verification method explains"),
      "Ask response was not human-readable.",
    );
    assert(
      !ask.assistantMessage.includes("Network or parsing error"),
      "Ask response fell back to network/parsing error.",
    );
    assert(ask.boardAction?.type === "none", "Ask response changed the board.");
    console.log("PASS ask");

    const plan = await postJson(`${appBaseUrl}/api/chat`, {
      messages: [{ role: "user", content: "@SRS plan Break this into review tasks." }],
      workPackages: seed,
      selectedWorkPackageId: "wp-srs",
      parsedCommand: {
        referencedPackageName: "System Requirements Specification",
        mode: "plan",
        instruction: "Break this into review tasks.",
      },
      llmConfig,
    });
    assert(
      typeof plan.assistantMessage === "string" &&
        plan.assistantMessage.includes("Planned next steps for SRS"),
      "Plan response message mismatch.",
    );
    assert(plan.boardAction?.type === "update", "Plan did not return an update.");
    assert(plan.boardAction?.workPackageId === "wp-srs", "Plan updated the wrong package.");
    assert(
      Array.isArray(plan.boardAction?.fields?.tasks) &&
        plan.boardAction.fields.tasks.length > 0,
      "Plan did not provide tasks.",
    );
    console.log("PASS plan");

    const change = await postJson(`${appBaseUrl}/api/chat`, {
      messages: [
        { role: "user", content: "@SRS change Add cybersecurity acceptance criteria." },
      ],
      workPackages: seed,
      selectedWorkPackageId: "wp-srs",
      parsedCommand: {
        referencedPackageName: "System Requirements Specification",
        mode: "change",
        instruction: "Add cybersecurity acceptance criteria.",
      },
      llmConfig,
    });
    assert(
      typeof change.assistantMessage === "string" &&
        change.assistantMessage.includes("Updated SRS"),
      "Change response message mismatch.",
    );
    assert(change.boardAction?.type === "update", "Change did not return an update.");
    assert(
      change.boardAction?.workPackageId === "wp-srs",
      "Change updated the wrong package.",
    );
    assert(
      String(change.boardAction?.fields?.objective || "").includes(
        "Add cybersecurity acceptance criteria.",
      ),
      "Change response did not update the objective.",
    );
    console.log("PASS change");

    console.log("Smoke test complete.");
  } finally {
    await new Promise((resolve, reject) => {
      server.close((error) => {
        if (error) {
          reject(error);
          return;
        }
        resolve();
      });
    });
  }
}

main().catch((error) => {
  console.error(error instanceof Error ? error.message : String(error));
  process.exitCode = 1;
});