Skip to content
File

Blob: src/cloudflare/internal/test/to-markdown/to-markdown-api-test.js

javascript136 lines
1// Copyright (c) 2024 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import assert from 'node:assert';
6 
7export const tests = {
8 async test(_, env) {
9 {
10 // Test toMarkdown with single file
11 const resp = await env.ai.toMarkdown(
12 {
13 name: 'random.md',
14 blob: new Blob([`# Random Markdown`], { type: 'text/markdown' }),
15 },
16 { gateway: { id: 'my-gateway' } }
17 );
18 
19 assert.deepStrictEqual(resp, {
20 name: 'random.md',
21 mimeType: 'text/markdown',
22 format: 'markdown',
23 tokens: 0,
24 data: '# Random Markdown',
25 });
26 }
27 
28 {
29 // Test toMarkdown with extra headers
30 const resp = await env.ai.toMarkdown(
31 {
32 name: 'headers.md',
33 blob: new Blob([`# Random Markdown`], { type: 'text/markdown' }),
34 },
35 { extraHeaders: { example: 'header' } }
36 );
37 
38 assert.deepStrictEqual(resp, {
39 name: 'headers.md',
40 mimeType: 'text/markdown',
41 format: 'markdown',
42 tokens: 0,
43 data: { 'content-type': 'application/json', example: 'header' },
44 });
45 }
46 
47 {
48 // Test toMarkdown with multiple file
49 const resp = await env.ai.toMarkdown([
50 {
51 name: 'random.md',
52 blob: new Blob([`# Random Markdown`], { type: 'text/markdown' }),
53 },
54 {
55 name: 'cat.png',
56 blob: new Blob(
57 [
58 `The image shows a white and orange cat standing on a beige floor`,
59 ],
60 { type: 'image/png' }
61 ),
62 },
63 ]);
64 
65 assert.deepStrictEqual(resp, [
66 {
67 name: 'random.md',
68 mimeType: 'text/markdown',
69 format: 'markdown',
70 tokens: 0,
71 data: '# Random Markdown',
72 },
73 {
74 name: 'cat.png',
75 mimeType: 'image/png',
76 format: 'markdown',
77 tokens: 0,
78 data: 'The image shows a white and orange cat standing on a beige floor',
79 },
80 ]);
81 }
82 
83 {
84 // Test toMarkdown using the exported service
85 const resp = await env.ai.toMarkdown().transform([
86 {
87 name: 'random.md',
88 blob: new Blob([`# Random Markdown`], { type: 'text/markdown' }),
89 },
90 {
91 name: 'cat.png',
92 blob: new Blob(
93 [
94 `The image shows a white and orange cat standing on a beige floor`,
95 ],
96 { type: 'image/png' }
97 ),
98 },
99 ]);
100 
101 assert.deepStrictEqual(resp, [
102 {
103 name: 'random.md',
104 mimeType: 'text/markdown',
105 format: 'markdown',
106 tokens: 0,
107 data: '# Random Markdown',
108 },
109 {
110 name: 'cat.png',
111 mimeType: 'image/png',
112 format: 'markdown',
113 tokens: 0,
114 data: 'The image shows a white and orange cat standing on a beige floor',
115 },
116 ]);
117 }
118 
119 {
120 // Test supported endpoint of ToMarkdown service
121 const resp = await env.ai.toMarkdown().supported();
122 
123 assert.deepStrictEqual(resp, [
124 {
125 extension: '.md',
126 mimeType: 'text/markdown',
127 },
128 {
129 extension: '.png',
130 mimeType: 'image/png',
131 },
132 ]);
133 }
134 },
135};