From 6b7ec1dd739bc0f92db03fef1af0b39099f261c5 Mon Sep 17 00:00:00 2001 From: takejohn Date: Sun, 5 Jul 2026 01:21:27 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E6=99=82=E3=81=AB?= =?UTF-8?q?=E6=8B=A1=E5=BC=B5=E6=A9=9F=E8=83=BD=E3=81=AE=E5=AE=9A=E6=95=B0?= =?UTF-8?q?=E3=81=8C=E6=B3=A8=E5=85=A5=E3=81=95=E3=82=8C=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/aisenv/src/cli/test/context.ts | 11 ++++-- packages/aisenv/src/cli/test/test.ts | 7 ++-- packages/aisenv/tests/cli/test/test.ts | 50 ++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/packages/aisenv/src/cli/test/context.ts b/packages/aisenv/src/cli/test/context.ts index 45bbae4..0545660 100644 --- a/packages/aisenv/src/cli/test/context.ts +++ b/packages/aisenv/src/cli/test/context.ts @@ -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 { try { diff --git a/packages/aisenv/src/cli/test/test.ts b/packages/aisenv/src/cli/test/test.ts index b2a4008..bae2586 100644 --- a/packages/aisenv/src/cli/test/test.ts +++ b/packages/aisenv/src/cli/test/test.ts @@ -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 { - const context = new Context(filename); +async function runTest(filename: string, config: Config): Promise { + const context = new Context(filename, config); const testFile = await context.prepareTestFile(); if (testFile.state == 'error') { return { @@ -28,7 +29,7 @@ export async function test(): Promise { 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; } diff --git a/packages/aisenv/tests/cli/test/test.ts b/packages/aisenv/tests/cli/test/test.ts index e10bb9e..eae808c 100644 --- a/packages/aisenv/tests/cli/test/test.ts +++ b/packages/aisenv/tests/cli/test/test.ts @@ -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 { @@ -17,8 +18,10 @@ const mockResolveConfig = vi.hoisted(() => vi.fn(), ); -vi.mock('../../../src/common/config.js', async () => { +vi.mock('../../../src/common/config.js', async (importOriginal) => { + const original = await importOriginal(); return { + ...original, resolveConfig: mockResolveConfig, }; }); @@ -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(); + }); + }); });