Repository navigation
Expand file tree
/
Copy pathqa-native-elements-fixture.mts
More file actions
98 lines (97 loc) · 2.68 KB
/
Copy pathqa-native-elements-fixture.mts
File metadata and controls
98 lines (97 loc) · 2.68 KB
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
// Imported only by the isolated in-memory QA server.
import { randomUUID } from "node:crypto";
import sharp from "sharp";
import { generateImageAsset } from "../apps/server/src/ai/images.js";
import { insertGeneratedImage } from "../apps/server/src/ai/image-insert.js";
import {
readAIDocument,
editAIDocument,
} from "../packages/core/src/workflows/ai-documents.js";
import type { DB } from "../packages/db/src/index.js";
import type { Actor } from "../packages/core/src/modules/identity/passwords.js";
import type { StorageRuntime } from "../apps/server/src/adapters/storage.js";
export async function seedNativeElements(
db: DB,
actor: Actor,
docs: { id: string; format: string }[],
storage: StorageRuntime,
) {
if (process.env.DOCA_QA_ISOLATED !== "1")
throw Error("Isolated fixtures only");
const image = await generateImageAsset(
db,
{ actor },
{ prompt: "隔离图片元素测试" },
randomUUID(),
{
storage,
fetch: (async () =>
Response.json({
data: [
{
b64_json: (
await sharp({
create: {
width: 320,
height: 200,
channels: 3,
background: "#248a66",
},
})
.png()
.toBuffer()
).toString("base64"),
},
],
})) as typeof fetch,
},
);
for (const doc of docs) {
if (doc.format !== "spreadsheet")
await insertGeneratedImage(
db,
{ actor },
{ assetId: image.assetId, resourceId: doc.id },
randomUUID(),
storage,
);
const read = await readAIDocument(db, { actor }, doc.id);
if (doc.format === "rich_text") {
await editAIDocument(
db,
{ actor },
doc.id,
{ seq: read.seq, epochId: read.epochId! },
[
{
type: "insertBlock",
block: {
id: "qa-code-block",
type: "code-block",
language: "go",
code: 'package main\n\nfunc main() {\n println("native code block")\n}',
children: [{ text: "" }],
},
},
],
randomUUID(),
);
} else if (doc.format === "spreadsheet") {
const value = read.value as any;
await editAIDocument(
db,
{ actor },
doc.id,
{ seq: read.seq, epochId: read.epochId! },
[
{
type: "cells",
sheetId: value.sheetOrder[0],
cells: { 0: { 0: { v: "原生单元格" }, 1: { v: 12 } } },
},
],
randomUUID(),
);
}
}
}