98 lines
3.9 KiB
TypeScript
98 lines
3.9 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { loadYarnProject, loadYarnProjectSync, LoadError } from '../src/loader/index';
|
|
import { resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
|
|
|
describe('loadYarnProject', () => {
|
|
it('should load a simple project', async () => {
|
|
const projectPath = resolve(__dirname, 'fixtures/simple/.yarnproject');
|
|
const result = await loadYarnProject(projectPath);
|
|
|
|
expect(result.project.projectFileVersion).toBe(4);
|
|
expect(result.project.projectName).toBe('Simple Test Project');
|
|
expect(result.project.sourceFiles).toEqual(['**/*.yarn']);
|
|
expect(result.project.baseLanguage).toBe('en');
|
|
expect(Object.keys(result.program.nodes).length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should compile all yarn files in simple project', async () => {
|
|
const projectPath = resolve(__dirname, 'fixtures/simple/.yarnproject');
|
|
const result = await loadYarnProject(projectPath);
|
|
|
|
const titles = Object.keys(result.program.nodes);
|
|
expect(titles).toContain('Start');
|
|
expect(titles).toContain('Greeting');
|
|
expect(titles).toContain('AnotherNode');
|
|
});
|
|
|
|
it('should load a localised project', async () => {
|
|
const projectPath = resolve(__dirname, 'fixtures/localised/.yarnproject');
|
|
const result = await loadYarnProject(projectPath);
|
|
|
|
expect(result.project.projectName).toBe('Localised Project');
|
|
expect(result.project.baseLanguage).toBe('en');
|
|
expect(result.project.localisation).toBeDefined();
|
|
expect(result.project.localisation!['zh-Hans']).toBeDefined();
|
|
expect(Object.keys(result.program.nodes).length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should load a complex project with multiple patterns', async () => {
|
|
const projectPath = resolve(__dirname, 'fixtures/complex/.yarnproject');
|
|
const result = await loadYarnProject(projectPath);
|
|
|
|
expect(result.project.projectFileVersion).toBe(4);
|
|
expect(result.project.authorName).toEqual(['Author One', 'Author Two']);
|
|
expect(result.project.sourceFiles).toContain('dialogue/**/*.yarn');
|
|
expect(result.project.sourceFiles).toContain('scripts/**/*.yarn');
|
|
});
|
|
|
|
it('should parse yarn documents correctly', async () => {
|
|
const projectPath = resolve(__dirname, 'fixtures/simple/.yarnproject');
|
|
const result = await loadYarnProject(projectPath);
|
|
|
|
const startNode = result.program.nodes['Start'];
|
|
expect(startNode).toBeDefined();
|
|
expect(startNode && 'instructions' in startNode ? startNode.instructions.length : 0).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should handle nodes with tags', async () => {
|
|
const projectPath = resolve(__dirname, 'fixtures/simple/.yarnproject');
|
|
const result = await loadYarnProject(projectPath);
|
|
|
|
const greetingNode = result.program.nodes['Greeting'];
|
|
expect(greetingNode).toBeDefined();
|
|
});
|
|
|
|
it('should throw LoadError for non-existent file', async () => {
|
|
const projectPath = resolve(__dirname, 'fixtures/nonexistent/.yarnproject');
|
|
|
|
await expect(loadYarnProject(projectPath)).rejects.toThrow(LoadError);
|
|
});
|
|
|
|
it('should throw error for invalid JSON', async () => {
|
|
const projectPath = resolve(__dirname, 'fixtures/invalid/.yarnproject');
|
|
|
|
await expect(loadYarnProject(projectPath)).rejects.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('loadYarnProjectSync', () => {
|
|
it('should load a simple project synchronously', () => {
|
|
const projectPath = resolve(__dirname, 'fixtures/simple/.yarnproject');
|
|
const result = loadYarnProjectSync(projectPath);
|
|
|
|
expect(result.project.projectFileVersion).toBe(4);
|
|
expect(Object.keys(result.program.nodes).length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should compile all yarn files synchronously', () => {
|
|
const projectPath = resolve(__dirname, 'fixtures/simple/.yarnproject');
|
|
const result = loadYarnProjectSync(projectPath);
|
|
|
|
const titles = Object.keys(result.program.nodes);
|
|
expect(titles).toContain('Start');
|
|
});
|
|
});
|