Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/aisenv/src/cli/test/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import path from 'path';
import { Ast, Interpreter, Parser, utils, values } from "@syuilo/aiscript";
import { TestCase, TestCaseResult, TestFailure, TestPassed, TestResult } from './types.js';
import { TestFileError, TestFileReady } from './types.js';
import { Config } from '../../api/config.js';
import { collectConsts } from '../../common/config.js';

export class Context {
private readonly interpreter = new Interpreter({});
private readonly interpreter: Interpreter;

public constructor(
public readonly filename: string
) {}
public readonly filename: string,
config: Config,
) {
this.interpreter = new Interpreter(collectConsts(config));
}

public async prepareTestFile(): Promise<TestFileReady | TestFileError> {
try {
Expand Down
7 changes: 4 additions & 3 deletions packages/aisenv/src/cli/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { glob } from 'glob';
import { TestFileExecuted, TestFileResult } from './types.js';
import { SimpleReporter } from './reporter.js';
import { Context } from './context.js';
import { Config } from '../../api/config.js';

async function runTest(filename: string): Promise<TestFileResult> {
const context = new Context(filename);
async function runTest(filename: string, config: Config): Promise<TestFileResult> {
const context = new Context(filename, config);
const testFile = await context.prepareTestFile();
if (testFile.state == 'error') {
return {
Expand All @@ -28,7 +29,7 @@ export async function test(): Promise<boolean> {
const reporter = new SimpleReporter();
let passed = true;
for (const name of await glob(config.test.include)) {
const result = await runTest(name);
const result = await runTest(name, config);
if (result.result.state != 'executed' || result.result.children.some((result) => !result.result.passed)) {
passed = false;
}
Expand Down
50 changes: 49 additions & 1 deletion packages/aisenv/tests/cli/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
import * as modTest from '../../../src/cli/test/test.js';
import { Config } from '../../../src/api/config.js';
import mockFs from 'mock-fs';
import { values } from '@syuilo/aiscript';

vi.mock('node:url', async () => {
return {
Expand All @@ -17,8 +18,10 @@ const mockResolveConfig = vi.hoisted(() =>
vi.fn<typeof import('../../../src/common/config.js').resolveConfig>(),
);

vi.mock('../../../src/common/config.js', async () => {
vi.mock('../../../src/common/config.js', async (importOriginal) => {
const original = await importOriginal<object>();
return {
...original,
resolveConfig: mockResolveConfig,
};
});
Expand Down Expand Up @@ -276,4 +279,49 @@ describe('test', () => {
expect(mockResolveConfig).toHaveBeenCalledOnce();
});
});

describe('addons', () => {
test('addon variables defined', async () => {
const customFn = vi.fn().mockReturnValueOnce(values.NULL);
mockResolveConfig.mockImplementationOnce(async () => ({
test: { include: ['*.test.ais'] },
addons: [
{
name: 'custom',
consts: { custom_fn: values.FN_NATIVE(customFn) },
},
],
}));
mockFs({
'main.test.ais': 'custom_fn()',
});
const result = await modTest.test();
expect(result).toBe(true);
expect(mockResolveConfig).toHaveBeenCalledOnce();
expect(customFn).toHaveBeenCalledOnce();
});

test('addon variables defined in imported script', async () => {
const customFn = vi.fn().mockReturnValueOnce(values.NULL);
mockResolveConfig.mockImplementationOnce(async () => ({
test: { include: ['*.test.ais'] },
addons: [
{
name: 'custom',
consts: { custom_fn: values.FN_NATIVE(customFn) },
},
],
}));
mockFs({
'main.ais': 'custom_fn()',
'main.test.ais': `
### imports ['main.ais']
`,
});
const result = await modTest.test();
expect(result).toBe(true);
expect(mockResolveConfig).toHaveBeenCalledOnce();
expect(customFn).toHaveBeenCalledOnce();
});
});
});
Loading