File size: 7,023 Bytes
c4ae742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67b9551
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ae742
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import Fastify from "fastify";
import fastifyMultipart from "@fastify/multipart";
import JSZip from "jszip";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { randomUUID } from "node:crypto";
import { afterEach, describe, expect, it } from "vitest";

import { registerAnalysisRoutes } from "../routes/analysis.js";
import type { DetectedMode, ItemRow, JobRow, Store } from "../queue/store.js";

function now() {
  return Date.now();
}

function createFakeStore(): Store {
  const jobs = new Map<string, JobRow>();
  const items = new Map<string, ItemRow>();

  return {
    close() {},
    createJob(optionsSnapshot: object) {
      const ts = now();
      const job: JobRow = {
        id: randomUUID(),
        status: "pending",
        options_snapshot: JSON.stringify(optionsSnapshot),
        created_at: ts,
        updated_at: ts,
      };
      jobs.set(job.id, job);
      return job;
    },
    getJob(id: string) {
      return jobs.get(id) ?? null;
    },
    setJobStatus(id, status) {
      const job = jobs.get(id);
      if (job) jobs.set(id, { ...job, status, updated_at: now() });
    },
    updateJobOptions(id, optionsSnapshot) {
      const job = jobs.get(id);
      if (job) {
        jobs.set(id, {
          ...job,
          options_snapshot: JSON.stringify(optionsSnapshot),
          updated_at: now(),
        });
      }
    },
    listResumableJobs() {
      return [];
    },
    recoverInterruptedJob() {},
    createItem(input: {
      jobId: string;
      ord: number;
      filename: string;
      uploadPath: string;
      detectedMode: DetectedMode;
    }) {
      const ts = now();
      const item: ItemRow = {
        id: randomUUID(),
        job_id: input.jobId,
        ord: input.ord,
        filename: input.filename,
        upload_path: input.uploadPath,
        detected_mode: input.detectedMode,
        status: "queued",
        evidence_path: null,
        task_package_path: null,
        judge_result_path: null,
        export_zip_path: null,
        error_preview: null,
        error_details_path: null,
        attempt_count: 0,
        last_stage: null,
        created_at: ts,
        updated_at: ts,
      };
      items.set(item.id, item);
      return item;
    },
    getItem(id) {
      return items.get(id) ?? null;
    },
    listItemsByJob(jobId) {
      return [...items.values()]
        .filter((item) => item.job_id === jobId)
        .sort((a, b) => a.ord - b.ord);
    },
    setItemStatus(id, status, errorPreview, meta) {
      const item = items.get(id);
      if (item) {
        items.set(id, {
          ...item,
          status,
          error_preview: errorPreview ?? item.error_preview,
          last_stage: meta?.lastStage ?? item.last_stage,
          error_details_path: meta?.errorDetailsPath ?? item.error_details_path,
          updated_at: now(),
        });
      }
    },
    setItemPaths(id, paths) {
      const item = items.get(id);
      if (item) {
        items.set(id, {
          ...item,
          evidence_path: paths.evidencePath ?? item.evidence_path,
          task_package_path: paths.taskPackagePath ?? item.task_package_path,
          judge_result_path: paths.judgeResultPath ?? item.judge_result_path,
          export_zip_path: paths.exportZipPath ?? item.export_zip_path,
          updated_at: now(),
        });
      }
    },
    takeNextQueuedItem() {
      return [...items.values()].find((item) => item.status === "queued") ?? null;
    },
    resetItemForRetry(id) {
      const item = items.get(id);
      if (item) items.set(id, { ...item, status: "queued", updated_at: now() });
    },
  };
}

async function buildApiLookingZip(): Promise<Buffer> {
  const zip = new JSZip();
  zip.file("network.json", JSON.stringify([{ url: "https://example.test/api" }]));
  return zip.generateAsync({ type: "nodebuffer" });
}

function multipartFileThenRequestedMode(file: Buffer) {
  const boundary = "----rl-auto-test-" + randomUUID();
  const payload = Buffer.concat([
    Buffer.from(
      `--${boundary}\r\n` +
        `Content-Disposition: form-data; name="files"; filename="api-looking.zip"\r\n` +
        "Content-Type: application/zip\r\n\r\n",
    ),
    file,
    Buffer.from(
      `\r\n--${boundary}\r\n` +
        'Content-Disposition: form-data; name="requestedMode"\r\n\r\n' +
        `mcp\r\n` +
        `--${boundary}--\r\n`,
    ),
  ]);
  return {
    payload,
    contentType: `multipart/form-data; boundary=${boundary}`,
  };
}

describe("analysis upload requestedMode", () => {
  let tmpRoot: string | null = null;

  afterEach(() => {
    if (tmpRoot) rmSync(tmpRoot, { recursive: true, force: true });
    tmpRoot = null;
  });

  it("uses requestedMode for every uploaded file even when the field arrives after files", async () => {
    tmpRoot = mkdtempSync(join(tmpdir(), "rl-auto-upload-mode-"));
    const app = Fastify();
    await app.register(fastifyMultipart, {
      limits: { fileSize: 10 * 1024 * 1024, files: 10 },
    });
    await registerAnalysisRoutes(app, {
      store: createFakeStore(),
      config: {
        uploadDir: tmpRoot,
        maxBatchFiles: 10,
        maxUploadMB: 10,
      } as any,
      runner: {} as any,
    });

    const { payload, contentType } = multipartFileThenRequestedMode(
      await buildApiLookingZip(),
    );
    const res = await app.inject({
      method: "POST",
      url: "/api/analysis/jobs",
      headers: { "content-type": contentType },
      payload,
    });

    await app.close();

    expect(res.statusCode).toBe(200);
    expect(res.json()).toMatchObject({
      code: 0,
      data: {
        items: [expect.objectContaining({ detectedMode: "mcp" })],
      },
    });
  });

  it("clamps oversized numeric options when starting a job", async () => {
    tmpRoot = mkdtempSync(join(tmpdir(), "rl-auto-options-"));
    const store = createFakeStore();
    const job = await store.createJob({});
    const scheduledJobs: string[] = [];
    const app = Fastify();
    await registerAnalysisRoutes(app, {
      store,
      config: {
        uploadDir: tmpRoot,
        maxBatchFiles: 10,
        maxUploadMB: 10,
      } as any,
      runner: {
        scheduleJob(id: string) {
          scheduledJobs.push(id);
        },
        cancelJob() {},
      } as any,
    });

    const res = await app.inject({
      method: "POST",
      url: `/api/analysis/jobs/${job.id}/start`,
      headers: { "content-type": "application/json" },
      payload: {
        optionsSnapshot: {
          requestedMode: "api",
          concurrency: 99,
          temperature: 9,
          topP: 9,
          maxTokens: 999999,
        },
      },
    });

    await app.close();

    expect(res.statusCode).toBe(200);
    expect(scheduledJobs).toEqual([job.id]);
    expect(JSON.parse((await store.getJob(job.id))?.options_snapshot || "{}")).toMatchObject({
      concurrency: 8,
      temperature: 2,
      topP: 1,
      maxTokens: 131072,
    });
  });
});