168 lines
4.2 KiB
TypeScript
168 lines
4.2 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { validateYarnProject, isYarnProject } from '../src/loader/validator';
|
|
|
|
describe('validateYarnProject', () => {
|
|
it('should validate a minimal valid config', () => {
|
|
const config = {
|
|
projectFileVersion: 4,
|
|
sourceFiles: ['**/*.yarn'],
|
|
baseLanguage: 'en',
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(true);
|
|
});
|
|
|
|
it('should validate a full config', () => {
|
|
const config = {
|
|
projectFileVersion: 4,
|
|
projectName: 'Test Project',
|
|
authorName: ['Author 1', 'Author 2'],
|
|
sourceFiles: ['**/*.yarn'],
|
|
excludeFiles: ['**/*.backup.yarn'],
|
|
baseLanguage: 'en',
|
|
localisation: {
|
|
'zh-Hans': {
|
|
strings: 'translations/zh-Hans.csv',
|
|
assets: 'translations/zh-Hans/',
|
|
},
|
|
},
|
|
definitions: ['custom.ysls.json'],
|
|
compilerOptions: {
|
|
requireVariableDeclarations: true,
|
|
allowPreviewFeatures: false,
|
|
},
|
|
editorOptions: {
|
|
yarnScriptEditor: { theme: 'dark' },
|
|
},
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(true);
|
|
});
|
|
|
|
it('should reject missing projectFileVersion', () => {
|
|
const config = {
|
|
sourceFiles: ['**/*.yarn'],
|
|
baseLanguage: 'en',
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(false);
|
|
if (!result.valid) {
|
|
expect(result.errors.length).toBeGreaterThan(0);
|
|
expect(result.errors[0].message).toContain('projectFileVersion');
|
|
}
|
|
});
|
|
|
|
it('should reject missing sourceFiles', () => {
|
|
const config = {
|
|
projectFileVersion: 4,
|
|
baseLanguage: 'en',
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(false);
|
|
});
|
|
|
|
it('should reject missing baseLanguage', () => {
|
|
const config = {
|
|
projectFileVersion: 4,
|
|
sourceFiles: ['**/*.yarn'],
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(false);
|
|
});
|
|
|
|
it('should reject invalid projectFileVersion type', () => {
|
|
const config = {
|
|
projectFileVersion: '4',
|
|
sourceFiles: ['**/*.yarn'],
|
|
baseLanguage: 'en',
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(false);
|
|
});
|
|
|
|
it('should reject projectFileVersion less than 2', () => {
|
|
const config = {
|
|
projectFileVersion: 1,
|
|
sourceFiles: ['**/*.yarn'],
|
|
baseLanguage: 'en',
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(false);
|
|
});
|
|
|
|
it('should reject non-array sourceFiles', () => {
|
|
const config = {
|
|
projectFileVersion: 4,
|
|
sourceFiles: '**/*.yarn',
|
|
baseLanguage: 'en',
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(false);
|
|
});
|
|
|
|
it('should reject additional properties', () => {
|
|
const config = {
|
|
projectFileVersion: 4,
|
|
sourceFiles: ['**/*.yarn'],
|
|
baseLanguage: 'en',
|
|
unknownField: 'value',
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(false);
|
|
});
|
|
|
|
it('should accept definitions as string', () => {
|
|
const config = {
|
|
projectFileVersion: 4,
|
|
sourceFiles: ['**/*.yarn'],
|
|
baseLanguage: 'en',
|
|
definitions: 'custom.ysls.json',
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(true);
|
|
});
|
|
|
|
it('should accept definitions as array of strings', () => {
|
|
const config = {
|
|
projectFileVersion: 4,
|
|
sourceFiles: ['**/*.yarn'],
|
|
baseLanguage: 'en',
|
|
definitions: ['custom1.ysls.json', 'custom2.ysls.json'],
|
|
};
|
|
|
|
const result = validateYarnProject(config);
|
|
expect(result.valid).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('isYarnProject', () => {
|
|
it('should return true for valid config', () => {
|
|
const config = {
|
|
projectFileVersion: 4,
|
|
sourceFiles: ['**/*.yarn'],
|
|
baseLanguage: 'en',
|
|
};
|
|
|
|
expect(isYarnProject(config)).toBe(true);
|
|
});
|
|
|
|
it('should return false for invalid config', () => {
|
|
const config = {
|
|
sourceFiles: ['**/*.yarn'],
|
|
baseLanguage: 'en',
|
|
};
|
|
|
|
expect(isYarnProject(config)).toBe(false);
|
|
});
|
|
});
|