feat: add admin accounts and image templates
This commit is contained in:
@@ -4,11 +4,14 @@ import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
authenticatePublicApiRequest,
|
||||
publicApiOwnerId,
|
||||
PublicApiAuthError,
|
||||
type PublicApiClient
|
||||
} from "@/lib/server/public-api-auth";
|
||||
import { createPublicGenerationJob, PublicApiConflictError } from "@/lib/server/public-api-jobs";
|
||||
import { claimGenerationJobs, createGenerationJob, getGenerationJob, listGenerationJobs } from "@/lib/server/data-store";
|
||||
import { getPublicApiAsset, listPublicApiAssets } from "@/lib/server/public-api-assets";
|
||||
import { submitImageJob } from "@/lib/server/generation-service";
|
||||
import { runWorkerTick } from "@/lib/server/task-manager";
|
||||
import { DEFAULT_OWNER_ID } from "@/lib/server/runtime";
|
||||
import { signWebhookBody } from "@/lib/server/webhook";
|
||||
@@ -97,6 +100,35 @@ describe("task management and public API helpers", () => {
|
||||
})).rejects.toBeInstanceOf(PublicApiConflictError);
|
||||
});
|
||||
|
||||
it("partitions public jobs by API account id", async () => {
|
||||
const request = new Request("http://local.test/api/v1/jobs", {
|
||||
headers: { "Idempotency-Key": "same-account-local-key" }
|
||||
});
|
||||
const body = {
|
||||
capability: "image.generate" as const,
|
||||
prompt: "生成一张账号隔离测试图"
|
||||
};
|
||||
const first = await createPublicGenerationJob({
|
||||
client: { id: "agent-a", key: "secret-a" },
|
||||
request,
|
||||
origin: "http://local.test",
|
||||
body
|
||||
});
|
||||
const second = await createPublicGenerationJob({
|
||||
client: { id: "agent-b", key: "secret-b" },
|
||||
request,
|
||||
origin: "http://local.test",
|
||||
body
|
||||
});
|
||||
|
||||
expect(first.job.ownerId).toBe(publicApiOwnerId("agent-a"));
|
||||
expect(second.job.ownerId).toBe(publicApiOwnerId("agent-b"));
|
||||
expect(first.job.id).not.toBe(second.job.id);
|
||||
expect(await listGenerationJobs(publicApiOwnerId("agent-a"), 10)).toHaveLength(1);
|
||||
expect(await listGenerationJobs(publicApiOwnerId("agent-b"), 10)).toHaveLength(1);
|
||||
expect(await listGenerationJobs(DEFAULT_OWNER_ID, 10)).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("passes EvoLink quality through public image jobs", async () => {
|
||||
process.env.IMAGE_GENERATE_ENGINE = "evolink";
|
||||
process.env.EVOLINK_MOCK = "true";
|
||||
@@ -118,6 +150,38 @@ describe("task management and public API helpers", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("allows image jobs to override the default generation engine", async () => {
|
||||
process.env.IMAGE_GENERATE_ENGINE = "evolink";
|
||||
process.env.EVOLINK_MOCK = "true";
|
||||
process.env.EVOLINK_API_KEY = "test-key";
|
||||
|
||||
const jimengJob = await submitImageJob({
|
||||
ownerId: DEFAULT_OWNER_ID,
|
||||
capability: "image.generate",
|
||||
engine: "jimeng",
|
||||
prompt: "即梦模板",
|
||||
scale: 70
|
||||
}, "http://local.test");
|
||||
expect(jimengJob.requestPayload.engine).toBe("jimeng");
|
||||
expect(jimengJob.requestPayload.providerPayload).toMatchObject({
|
||||
req_key: "jimeng_seedream46_cvtob",
|
||||
scale: 70
|
||||
});
|
||||
|
||||
const image2Job = await submitImageJob({
|
||||
ownerId: DEFAULT_OWNER_ID,
|
||||
capability: "image.generate",
|
||||
engine: "evolink",
|
||||
prompt: "Image2 模板",
|
||||
quality: "high"
|
||||
}, "http://local.test");
|
||||
expect(image2Job.requestPayload.engine).toBe("evolink");
|
||||
expect(image2Job.requestPayload.providerPayload).toMatchObject({
|
||||
model: "gpt-image-2",
|
||||
quality: "high"
|
||||
});
|
||||
});
|
||||
|
||||
it("claims local jobs without duplicate ownership", async () => {
|
||||
await Promise.all(Array.from({ length: 6 }, (_, index) => createGenerationJob({
|
||||
ownerId: DEFAULT_OWNER_ID,
|
||||
@@ -163,7 +227,12 @@ describe("task management and public API helpers", () => {
|
||||
expect(stored?.status).toBe("succeeded");
|
||||
expect(stored?.completedAt).toBeTruthy();
|
||||
expect(stored?.outputAssetIds.length).toBe(1);
|
||||
expect(await listGenerationJobs(DEFAULT_OWNER_ID, 10)).toHaveLength(1);
|
||||
expect(stored?.ownerId).toBe(publicApiOwnerId("agent-a"));
|
||||
expect(await listGenerationJobs(publicApiOwnerId("agent-a"), 10)).toHaveLength(1);
|
||||
expect(await listGenerationJobs(DEFAULT_OWNER_ID, 10)).toHaveLength(0);
|
||||
expect(await listPublicApiAssets("agent-a")).toHaveLength(1);
|
||||
expect(await listPublicApiAssets("agent-b")).toHaveLength(0);
|
||||
expect(await getPublicApiAsset("agent-b", stored!.outputAssetIds[0])).toBeNull();
|
||||
});
|
||||
|
||||
it("signs webhook bodies with the configured secret", () => {
|
||||
|
||||
Reference in New Issue
Block a user