feat: add local auth billing and usage management
This commit is contained in:
1 parent
f642b5e71f
commit
196fdde83f
119 files changed
+15695
-2650
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
createAsset,
|
||||
createGenerationJob,
|
||||
createImageTemplate,
|
||||
listAssets,
|
||||
listGenerationJobs,
|
||||
listImageTemplates,
|
||||
listUsageEvents,
|
||||
recordUsageEvent
|
||||
} from "@/lib/server/data-store";
|
||||
import {
|
||||
createPlatformOrganization,
|
||||
createPlatformUser,
|
||||
deletePlatformUser,
|
||||
getPlatformUserById
|
||||
} from "@/lib/server/account-store";
|
||||
import type { PlatformUserRecord } from "@/lib/types";
|
||||
|
||||
let dataDirectory = "";
|
||||
let user: PlatformUserRecord;
|
||||
let archiveOwnerId = "";
|
||||
|
||||
describe("platform account ownership lifecycle", () => {
|
||||
beforeEach(async () => {
|
||||
dataDirectory = await mkdtemp(join(tmpdir(), "zhinian-account-store-"));
|
||||
vi.stubEnv("ZHINIAN_DATA_DIR", dataDirectory);
|
||||
vi.stubEnv("ZHINIAN_AUTH_REQUIRED", "1");
|
||||
vi.stubEnv("ZHINIAN_AUTH_SESSION_SECRET", "test-platform-session-secret");
|
||||
vi.stubEnv("NEXT_PUBLIC_SUPABASE_URL", "");
|
||||
vi.stubEnv("SUPABASE_SERVICE_ROLE_KEY", "");
|
||||
const organization = await createPlatformOrganization("归档组织");
|
||||
archiveOwnerId = organization.archiveOwnerId;
|
||||
user = await createPlatformUser({
|
||||
phone: "13800138000",
|
||||
displayName: "待删除用户",
|
||||
password: "ArchivePass123",
|
||||
role: "user",
|
||||
organizationId: organization.id
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
vi.unstubAllEnvs();
|
||||
await rm(dataDirectory, { force: true, recursive: true });
|
||||
});
|
||||
|
||||
it("removes login identity, archives business data, and retains usage history", async () => {
|
||||
const asset = await createAsset({
|
||||
ownerId: user.id,
|
||||
kind: "image",
|
||||
name: "待归档素材",
|
||||
url: "https://example.com/archive.png",
|
||||
source: "upload",
|
||||
tags: [],
|
||||
metadata: {}
|
||||
});
|
||||
const job = await createGenerationJob({
|
||||
ownerId: user.id,
|
||||
capability: "image.generate",
|
||||
provider: "evolink",
|
||||
reqKey: "gpt-image-2",
|
||||
status: "succeeded",
|
||||
inputAssetIds: [asset.id],
|
||||
inputUrls: [],
|
||||
outputAssetIds: [asset.id],
|
||||
requestPayload: {}
|
||||
});
|
||||
await createImageTemplate({
|
||||
ownerId: user.id,
|
||||
name: "待归档模板",
|
||||
prompt: "测试提示词",
|
||||
settings: {},
|
||||
sortOrder: 0
|
||||
});
|
||||
await recordUsageEvent({
|
||||
ownerId: user.id,
|
||||
jobId: job.id,
|
||||
source: "platform",
|
||||
capability: job.capability,
|
||||
provider: job.provider,
|
||||
quantity: 1,
|
||||
estimatedUnit: "image"
|
||||
});
|
||||
|
||||
await deletePlatformUser(user.id);
|
||||
|
||||
expect(await getPlatformUserById(user.id, { includeDisabled: true })).toBeNull();
|
||||
expect((await listAssets(archiveOwnerId)).map((item) => item.id)).toEqual([asset.id]);
|
||||
expect((await listGenerationJobs(archiveOwnerId)).map((item) => item.id)).toEqual([job.id]);
|
||||
expect((await listImageTemplates(archiveOwnerId)).map((item) => item.name)).toEqual(["待归档模板"]);
|
||||
expect((await listUsageEvents()).find((event) => event.jobId === job.id)).toMatchObject({ ownerId: user.id });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user