feat(P0-A): 整合第一版并冻结最终发布 #1
@@ -0,0 +1,109 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
|
||||
import {
|
||||
WP7_02_CONTROLLED_REAL_LIMIT,
|
||||
buildControlledExecutionPlan,
|
||||
buildProviderRequest,
|
||||
buildSanitizedResponseEvidence,
|
||||
normalizeProviderResponse,
|
||||
validateSanitizedEvidence,
|
||||
} from "../../scripts/lib/wp7-02-controlled-executor.mjs";
|
||||
|
||||
const models = [
|
||||
{
|
||||
config_version: 1,
|
||||
model_id: "gemini-3.1-flash-image-preview",
|
||||
route_profile: {
|
||||
endpoint: "https://oneapi.intelligrow.cn/v1beta/models/gemini-3.1-flash-image-preview:generateContent",
|
||||
mode: "sync",
|
||||
protocol_version: "gemini-native-v1beta",
|
||||
},
|
||||
},
|
||||
{
|
||||
config_version: 1,
|
||||
model_id: "gemini-3-pro-image-preview",
|
||||
route_profile: {
|
||||
endpoint: "https://oneapi.intelligrow.cn/v1beta/models/gemini-3-pro-image-preview:generateContent",
|
||||
mode: "sync",
|
||||
protocol_version: "gemini-native-v1beta",
|
||||
},
|
||||
},
|
||||
{
|
||||
config_version: 1,
|
||||
model_id: "gpt-image-2",
|
||||
route_profile: {
|
||||
endpoint: "https://oneapi.intelligrow.cn/v1/images/generations",
|
||||
mode: "sync",
|
||||
protocol_version: "openai-images-v1",
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const onePixelPng = Buffer.from(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=",
|
||||
"base64",
|
||||
);
|
||||
|
||||
test("TDD-WP7-EXT-001 caps real calls and separates real from deterministic scenarios", () => {
|
||||
const plans = models.map((model) => buildControlledExecutionPlan(model));
|
||||
assert.equal(WP7_02_CONTROLLED_REAL_LIMIT, 120);
|
||||
assert.equal(plans.reduce((total, plan) => total + plan.planned_real_calls, 0) <= WP7_02_CONTROLLED_REAL_LIMIT, true);
|
||||
for (const plan of plans) {
|
||||
assert.deepEqual(plan.real_scenarios.map((entry) => entry.ratio), ["3:4", "1:1", "4:3", "9:16", "1:1"]);
|
||||
assert.deepEqual(plan.real_scenarios.map((entry) => entry.input), ["pure_text", "pure_text", "pure_text", "pure_text", "reference_image"]);
|
||||
assert.deepEqual(plan.execution_modes, [
|
||||
{ mode: "sync", source: "real_gateway" },
|
||||
{ mode: "async", source: "deterministic_local" },
|
||||
{ mode: "poll", source: "deterministic_local" },
|
||||
]);
|
||||
assert.equal(plan.error_scenarios.length, 9);
|
||||
assert.equal(plan.error_scenarios.every((entry) => entry.source === "deterministic_local"), true);
|
||||
assert.deepEqual(plan.state_scenarios.map((entry) => entry.name), [
|
||||
"credit_commit_once",
|
||||
"credit_release_once_per_terminal_failure",
|
||||
"contract_change_invalidation",
|
||||
"full_revalidation",
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
test("TDD-WP7-EXT-001 builds protocol-specific requests without auth in arguments", () => {
|
||||
const reference = { bytes: onePixelPng, mime_type: "image/png" };
|
||||
const gemini = buildProviderRequest({ modelConfig: models[0], prompt: "controlled fixture prompt", ratio: "3:4", reference });
|
||||
assert.equal(gemini.method, "POST");
|
||||
assert.equal(gemini.body.contents[0].parts.some((part) => part.inlineData?.data), true);
|
||||
assert.equal(gemini.body.generationConfig.imageConfig.aspectRatio, "3:4");
|
||||
assert.equal("authorization" in gemini.headers, false);
|
||||
|
||||
const openai = buildProviderRequest({ modelConfig: models[2], prompt: "controlled fixture prompt", ratio: "9:16" });
|
||||
assert.deepEqual(openai.body, {
|
||||
model: "gpt-image-2",
|
||||
prompt: "controlled fixture prompt",
|
||||
response_format: "b64_json",
|
||||
size: "1024x1536",
|
||||
});
|
||||
assert.equal("authorization" in openai.headers, false);
|
||||
});
|
||||
|
||||
test("TDD-WP7-EXT-001 retains only response metadata and hashes", () => {
|
||||
const geminiResponse = {
|
||||
candidates: [{ content: { parts: [{ inlineData: { data: onePixelPng.toString("base64"), mimeType: "image/png" } }] } }],
|
||||
usageMetadata: { candidatesTokenCount: 7, promptTokenCount: 5, totalTokenCount: 12 },
|
||||
};
|
||||
const normalized = normalizeProviderResponse(models[0], geminiResponse);
|
||||
const evidence = buildSanitizedResponseEvidence(normalized);
|
||||
assert.deepEqual(evidence.dimensions, { height: 1, width: 1 });
|
||||
assert.equal(evidence.mime, "image/png");
|
||||
assert.match(evidence.evidence_hash, /^sha256:[A-F0-9]{64}$/);
|
||||
assert.deepEqual(evidence.usage_summary, { input_units: 5, output_units: 7, total_units: 12 });
|
||||
assert.doesNotMatch(JSON.stringify(evidence), /iVBOR|bytes|data|prompt|authorization|token/i);
|
||||
assert.equal(validateSanitizedEvidence(evidence), evidence);
|
||||
});
|
||||
|
||||
test("TDD-WP7-EXT-001 rejects sensitive or shared evidence fields", () => {
|
||||
for (const key of ["raw_prompt", "raw_provider_payload", "credential_value", "authorization", "absolute_path"]) {
|
||||
assert.throws(() => validateSanitizedEvidence({ [key]: "forbidden", status: "passed" }), /WP7_02_SENSITIVE_EVIDENCE_FORBIDDEN/);
|
||||
}
|
||||
assert.throws(() => validateSanitizedEvidence({ status: "passed", verified: true }), /WP7_02_SHARED_VERIFIED_FORBIDDEN/);
|
||||
});
|
||||
Reference in New Issue
Block a user