-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcloud-prompt.test.ts
More file actions
126 lines (115 loc) · 3.9 KB
/
cloud-prompt.test.ts
File metadata and controls
126 lines (115 loc) · 3.9 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
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
import { describe, expect, it } from "vitest";
import {
CLOUD_PROMPT_PREFIX,
deserializeCloudPrompt,
promptBlocksToText,
serializeCloudPrompt,
} from "./cloud-prompt";
describe("cloud-prompt", () => {
describe("serializeCloudPrompt", () => {
it("returns plain text for a single text block", () => {
const result = serializeCloudPrompt([
{ type: "text", text: " hello world " },
]);
expect(result).toBe("hello world");
expect(result).not.toContain(CLOUD_PROMPT_PREFIX);
});
it("returns prefixed JSON for multi-block content", () => {
const blocks = [
{ type: "text" as const, text: "read this" },
{
type: "resource" as const,
resource: {
uri: "attachment://test.txt",
text: "file contents",
mimeType: "text/plain",
},
},
];
const result = serializeCloudPrompt(blocks);
expect(result).toMatch(
new RegExp(
`^${CLOUD_PROMPT_PREFIX.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`,
),
);
const payload = JSON.parse(result.slice(CLOUD_PROMPT_PREFIX.length));
expect(payload.blocks).toEqual(blocks);
});
});
describe("deserializeCloudPrompt", () => {
it("round-trips with serializeCloudPrompt (text-only)", () => {
const original = [{ type: "text" as const, text: "hello" }];
const serialized = serializeCloudPrompt(original);
const deserialized = deserializeCloudPrompt(serialized);
expect(deserialized).toEqual(original);
});
it("round-trips with serializeCloudPrompt (multi-block)", () => {
const original = [
{ type: "text" as const, text: "read this" },
{
type: "resource" as const,
resource: {
uri: "attachment://test.txt",
text: "contents",
mimeType: "text/plain",
},
},
];
const serialized = serializeCloudPrompt(original);
const deserialized = deserializeCloudPrompt(serialized);
expect(deserialized).toEqual(original);
});
it("wraps plain string (no prefix) as a text block", () => {
const result = deserializeCloudPrompt("just a plain message");
expect(result).toEqual([{ type: "text", text: "just a plain message" }]);
});
it("returns empty array for empty string", () => {
expect(deserializeCloudPrompt("")).toEqual([]);
expect(deserializeCloudPrompt(" ")).toEqual([]);
});
it("falls back to text block for malformed JSON after prefix", () => {
const malformed = `${CLOUD_PROMPT_PREFIX}{not valid json`;
const result = deserializeCloudPrompt(malformed);
expect(result).toEqual([{ type: "text", text: malformed }]);
});
it("falls back to text block for empty blocks array", () => {
const payload = `${CLOUD_PROMPT_PREFIX}${JSON.stringify({ blocks: [] })}`;
const result = deserializeCloudPrompt(payload);
expect(result).toEqual([{ type: "text", text: payload }]);
});
});
describe("promptBlocksToText", () => {
it("extracts and joins text blocks", () => {
const result = promptBlocksToText([
{ type: "text", text: "hello " },
{
type: "resource",
resource: {
uri: "attachment://f.txt",
text: "ignored",
mimeType: "text/plain",
},
},
{ type: "text", text: "world" },
]);
expect(result).toBe("hello world");
});
it("returns empty string for non-text blocks only", () => {
expect(
promptBlocksToText([
{
type: "resource",
resource: {
uri: "attachment://f.txt",
text: "content",
mimeType: "text/plain",
},
},
]),
).toBe("");
});
it("returns empty string for empty array", () => {
expect(promptBlocksToText([])).toBe("");
});
});
});