|
| 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 | +}); |
0 commit comments