Skip to content

Commit ed5a6f0

Browse files
authored
feat: add Dojo as a source in cairo-coder (#90)
1 parent baa63e0 commit ed5a6f0

27 files changed

+2501
-40
lines changed

ingesters/__tests__/AsciiDocIngester.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('AsciiDocIngester', () => {
6666
bookConfig: {
6767
repoOwner: 'test-owner',
6868
repoName: 'test-repo',
69-
fileExtension: '.adoc',
69+
fileExtensions: ['.adoc'],
7070
chunkSize: 1000,
7171
chunkOverlap: 200,
7272
baseUrl: 'https://example.com',
@@ -300,7 +300,7 @@ This is page 2 content.`,
300300
bookConfig: {
301301
repoOwner: 'test-owner',
302302
repoName: 'test-repo',
303-
fileExtension: '.adoc',
303+
fileExtensions: ['.adoc'],
304304
chunkSize: 1000,
305305
chunkOverlap: 200,
306306
baseUrl: '',
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import { describe, it, expect } from 'bun:test';
2+
import { DojoDocsIngester } from '../src/ingesters/DojoDocsIngester';
3+
import { DocumentSource } from '../src/types';
4+
5+
describe('DojoDocsIngester', () => {
6+
it('should be configured correctly for Dojo documentation', () => {
7+
const ingester = new DojoDocsIngester();
8+
9+
// Check that it has the correct configuration
10+
expect(ingester).toBeDefined();
11+
// @ts-ignore - accessing config for testing purposes
12+
expect(ingester.config).toEqual({
13+
repoOwner: 'dojoengine',
14+
repoName: 'book',
15+
fileExtensions: ['.md', '.mdx'],
16+
chunkSize: 4096,
17+
chunkOverlap: 512,
18+
baseUrl: 'https://book.dojoengine.org',
19+
urlSuffix: '',
20+
useUrlMapping: true,
21+
});
22+
// @ts-ignore - accessing source for testing purposes
23+
expect(ingester.source).toBe(DocumentSource.DOJO_DOCS);
24+
});
25+
26+
it('should have correct extract directory', () => {
27+
const ingester = new DojoDocsIngester();
28+
29+
// @ts-ignore - accessing private method for testing
30+
const extractDir = ingester.getExtractDir();
31+
32+
expect(extractDir).toBeDefined();
33+
expect(extractDir).toContain('dojo-docs');
34+
});
35+
36+
it('should process markdown content correctly', () => {
37+
const ingester = new DojoDocsIngester();
38+
39+
// Test markdown content
40+
const testContent = `## Getting Started
41+
42+
Dojo is a provable game engine and toolchain for building onchain games.
43+
44+
\`\`\`bash
45+
$ sozo init my-game
46+
\`\`\`
47+
48+
This command creates a new Dojo project.
49+
50+
## Installation
51+
52+
Install the required dependencies.`;
53+
54+
// @ts-ignore - accessing private method for testing
55+
const chunks = ingester.createChunkFromPage('getting-started', testContent);
56+
57+
expect(chunks).toBeDefined();
58+
expect(chunks.length).toBeGreaterThan(0);
59+
60+
// Check that chunks have correct metadata
61+
const firstChunk = chunks[0];
62+
expect(firstChunk).toBeDefined();
63+
expect(firstChunk!.metadata.name).toBe('getting-started');
64+
expect(firstChunk!.metadata.source).toBe(DocumentSource.DOJO_DOCS);
65+
expect(firstChunk!.metadata.sourceLink).toContain(
66+
'https://book.dojoengine.org',
67+
);
68+
});
69+
70+
it('should split content into multiple sections based on headers', () => {
71+
const ingester = new DojoDocsIngester();
72+
73+
// Test content with multiple headers
74+
const processedContent = `## Introduction
75+
76+
Dojo is a provable game engine and toolchain for building onchain games.
77+
78+
## Installation
79+
80+
Install Dojo using the following command:
81+
82+
\`\`\`bash
83+
$ curl -L https://install.dojoengine.org | bash
84+
\`\`\`
85+
86+
## Configuration
87+
88+
Configure your Dojo project settings in the Scarb.toml file.`;
89+
90+
// @ts-ignore - accessing private method for testing
91+
const chunks = ingester.createChunkFromPage(
92+
'getting-started',
93+
processedContent,
94+
);
95+
96+
expect(chunks).toBeDefined();
97+
expect(chunks.length).toBeGreaterThanOrEqual(3);
98+
99+
// Check that we have chunks for each section
100+
const titles = chunks.map((chunk) => chunk.metadata.title);
101+
expect(titles).toContain('Introduction');
102+
expect(titles).toContain('Installation');
103+
expect(titles).toContain('Configuration');
104+
});
105+
106+
it('should sanitize code blocks correctly', () => {
107+
const ingester = new DojoDocsIngester();
108+
109+
// Test content with code blocks
110+
const testContent = `## Example
111+
112+
Here's an example:
113+
114+
\`\`\`cairo
115+
# This is a comment in code
116+
fn main() {
117+
println!("Hello");
118+
}
119+
\`\`\`
120+
121+
That was the example.`;
122+
123+
// @ts-ignore - accessing private method for testing
124+
const sanitized = ingester.sanitizeCodeBlocks(testContent);
125+
126+
expect(sanitized).toBeDefined();
127+
expect(sanitized).toContain('```cairo');
128+
expect(sanitized).toContain('fn main()');
129+
});
130+
131+
it('should generate correct URLs with useUrlMapping', () => {
132+
const ingester = new DojoDocsIngester();
133+
134+
const testContent = `## Components
135+
136+
Components are the building blocks of your Dojo entities.`;
137+
138+
// @ts-ignore - accessing private method for testing
139+
const chunks = ingester.createChunkFromPage(
140+
'core-concepts/entities',
141+
testContent,
142+
);
143+
144+
expect(chunks).toBeDefined();
145+
expect(chunks.length).toBeGreaterThan(0);
146+
147+
// All chunks should have the correct source link format
148+
chunks.forEach((chunk) => {
149+
expect(chunk.metadata.sourceLink).toContain(
150+
'https://book.dojoengine.org',
151+
);
152+
expect(chunk.metadata.sourceLink).toContain('core-concepts/entities');
153+
expect(chunk.metadata.source).toBe(DocumentSource.DOJO_DOCS);
154+
});
155+
});
156+
});

ingesters/__tests__/IngesterFactory.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe('IngesterFactory', () => {
7676
DocumentSource.SCARB_DOCS,
7777
DocumentSource.STARKNET_JS,
7878
DocumentSource.STARKNET_BLOG,
79+
DocumentSource.DOJO_DOCS,
7980
]);
8081
});
8182
});

ingesters/__tests__/MarkdownIngester.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const markdownIngester = new TestMarkdownIngester(
1515
repoOwner: 'test',
1616
repoName: 'test',
1717
baseUrl: 'https://test.com',
18-
fileExtension: 'md',
18+
fileExtensions: ['md'],
1919
urlSuffix: '.html',
2020
chunkSize: 1000,
2121
chunkOverlap: 100,
@@ -311,7 +311,7 @@ describe('URL sourcing and generation', () => {
311311
repoOwner: 'test',
312312
repoName: 'test',
313313
baseUrl: 'https://docs.example.com',
314-
fileExtension: 'md',
314+
fileExtensions: ['md'],
315315
urlSuffix: '.html',
316316
chunkSize: 1000,
317317
chunkOverlap: 100,
@@ -342,7 +342,7 @@ describe('URL sourcing and generation', () => {
342342
repoOwner: 'test',
343343
repoName: 'test',
344344
baseUrl: 'https://docs.starknet.io',
345-
fileExtension: 'md',
345+
fileExtensions: ['md'],
346346
urlSuffix: '',
347347
chunkSize: 1000,
348348
chunkOverlap: 100,
@@ -379,7 +379,7 @@ describe('URL sourcing and generation', () => {
379379
repoOwner: 'test',
380380
repoName: 'test',
381381
baseUrl: 'https://book.cairo-lang.org',
382-
fileExtension: 'md',
382+
fileExtensions: ['md'],
383383
urlSuffix: '.html',
384384
chunkSize: 1000,
385385
chunkOverlap: 100,

ingesters/__tests__/StarknetDocsIngester.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('StarknetDocsIngester', () => {
1212
expect(ingester.config).toEqual({
1313
repoOwner: 'starknet-io',
1414
repoName: 'starknet-docs',
15-
fileExtension: '.mdx',
15+
fileExtensions: ['.mdx'],
1616
chunkSize: 4096,
1717
chunkOverlap: 512,
1818
baseUrl: 'https://docs.starknet.io',

0 commit comments

Comments
 (0)