Skip to content

Commit 534150a

Browse files
committed
chore: setup testing and refine data layer types
- Configure Vitest and add unit tests for System core - Remove Codecov integration - Refine types in BillboardLayer, ChartsLayer, and FormulaData - Update CI workflow to run tests
1 parent a9649de commit 534150a

11 files changed

Lines changed: 534 additions & 17 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ jobs:
2525
- name: Build
2626
run: npm run build
2727

28+
- name: Test
29+
run: npm run test
30+
2831
- name: Type check
2932
run: npx tsc --noEmit
3033

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ node_modules
33
dist
44
/temp
55
package-lock.json
6-
yarn.lock
6+
coverage

examples/yarn.lock

Lines changed: 445 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"build": "tsup src/index.ts --format cjs,esm --dts",
2222
"pack": "npm pack",
2323
"test": "vitest run",
24+
"test:coverage": "vitest run --coverage",
2425
"lint": "eslint \"src/**/*.ts\"",
2526
"dev": "tsup src/index.ts --format cjs,esm --dts --watch"
2627
},
@@ -54,6 +55,7 @@
5455
},
5556
"sideEffects": false,
5657
"devDependencies": {
58+
"@vitest/coverage-v8": "^1.6.1",
5759
"tsup": "^8.0.0",
5860
"typescript": "^5.0.0",
5961
"vitest": "^1.0.0"

src/data/layers/BillboardLayer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { fx } from "../../core/system/System";
22
import { NodeType } from "../../core/types/NodeType";
33
import { EventManager as Eve } from "../../core/events/EventManager";
44
import { getCatchValue } from "../../utils/index";
5+
import { OperationLayerData } from "./OperationLayerData";
6+
import { MetadataData } from "../metadata/MetadataData";
7+
import { VariableValue } from "../models/VariableValue";
58

69
/**
710
* 元数据列表项的类型定义
@@ -22,9 +25,9 @@ export class BillboardLayer {
2225
/** 图层名称 */
2326
name: string = "";
2427
/** 运算层数组 */
25-
operationArray: any[] = [];
28+
operationArray: OperationLayerData[] = [];
2629
/** 元数据数组 */
27-
metadataArray: any[] = [];
30+
metadataArray: MetadataData[] = [];
2831
/** 唯一标识符 */
2932
id: number = 0;
3033

@@ -59,7 +62,7 @@ export class BillboardLayer {
5962
pushOperationLayer(): void {
6063
if (
6164
fx.clickBody != null &&
62-
fx.clickBody.type == NodeType.CalculationLayer
65+
fx.clickBody.type == NodeType.CalculationLayer
6366
) {
6467
const operationLayerData = new fx.OperationLayerData(fx.clickBody);
6568

src/data/layers/ChartsLayer.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
import { EventManager as Eve } from "../../core/events/EventManager";
33
import { fx } from "../../core/system/System";
4+
import { VariableValue } from "../models/VariableValue";
5+
import { OperationLayerData } from "./OperationLayerData";
46

57
/**
68
* 图表图层类,用于管理图表相关的运算层和元数据
@@ -11,12 +13,12 @@ export class ChartsLayer {
1113
name: string | undefined;
1214

1315
/** 运算层对象数组 */
14-
operationArray: any[] = [];
16+
operationArray: OperationLayerData[] = [];
1517

1618
/** 元数据对象 */
17-
metadata: any | null = null;
19+
metadata: VariableValue | null = null;
1820
/** 元数据数组 */
19-
metadataArray: any[] = [];
21+
metadataArray: VariableValue[] = [];
2022

2123
/** 初始单位值 */
2224
minValue: number = 1;
@@ -51,7 +53,7 @@ export class ChartsLayer {
5153
if (fx.clickBody != null) {
5254
this.metadata = (fx.clickBody as any).copy();
5355
// this.metadataArray.push(this.metadata);
54-
this.metadataArray[0] = this.metadata;
56+
this.metadataArray[0] = this.metadata as VariableValue;
5557
}
5658
}
5759

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
/**
1+
import { VariableValue } from "../models/VariableValue";
2+
3+
/**
24
* 运算层数据类,用于存储运算层的主体数据和视图信息
35
* @class OperationLayerData
46
*/
57
export class OperationLayerData {
68
/** 主体数据对象 */
7-
body: any;
9+
body: VariableValue;
810

911
/** 视图对象 */
1012
view: any = null;
@@ -13,7 +15,7 @@ export class OperationLayerData {
1315
* 创建运算层数据实例
1416
* @param body - 主体数据对象
1517
*/
16-
constructor(body: any) {
18+
constructor(body: VariableValue) {
1719
this.body = body.copy();
1820
}
1921
}

src/data/metadata/FormulaData.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { EventManager as Eve } from "../../core/events/EventManager";
33
import { getCatchValue } from "../../utils/index";
44
import { fx } from "../../core/system/System";
5+
import { BasicBody } from "../models/BasicBody";
56

67
/**
78
* 公式数据类,用于存储和管理公式相关信息
@@ -20,7 +21,7 @@ export class FormulaData {
2021
y: number | undefined;
2122

2223
/** 公式的主体数据对象 */
23-
body: any | null = null;
24+
body: BasicBody | null = null;
2425

2526
/** 公式的类型标识,固定为 "Formula" */
2627
type: string = "Formula";

src/data/metadata/MetadataData.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import { fx } from "../aextends/System";
1+
import { VariableValue } from "../models/VariableValue";
22

33
/**
44
* 元数据类,用于存储和管理元数据信息
@@ -8,7 +8,7 @@
88
*/
99
export class MetadataData {
1010
/** 元数据的主体对象 */
11-
body: any;
11+
body: VariableValue;
1212

1313
/** 元数据关联的视图对象 */
1414
view: any = null;
@@ -29,16 +29,16 @@ export class MetadataData {
2929
intervalValue: number = 0;
3030

3131
/** 元数据的列表数据 */
32-
list: any[];
32+
list: any[] = [];
3333

3434
/**
3535
* 创建元数据实例
3636
*
3737
* @constructor
38-
* @param {any} body - 主体数据对象
38+
* @param {VariableValue} body - 主体数据对象
3939
* @description 初始化元数据对象,复制主体数据作为副本
4040
*/
41-
constructor(body: any) {
41+
constructor(body: VariableValue) {
4242
this.body = body.copy();
4343
}
4444
}

tests/core/System.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { fx } from '../../src/core/system/System';
3+
4+
describe('fx (System)', () => {
5+
describe('Math Utilities', () => {
6+
it('distance should calculate Euclidean distance', () => {
7+
expect(fx.distance(0, 0, 3, 4)).toBe(5);
8+
expect(fx.distance(0, 0, 1, 1)).toBeCloseTo(1.41421356);
9+
});
10+
11+
it('lerp should interpolate between values', () => {
12+
expect(fx.lerp(0, 10, 0.5)).toBe(5);
13+
expect(fx.lerp(0, 10, 0)).toBe(0);
14+
expect(fx.lerp(0, 10, 1)).toBe(10);
15+
});
16+
17+
it('clamp should restrict values to range', () => {
18+
expect(fx.clamp(5, 0, 10)).toBe(5);
19+
expect(fx.clamp(-5, 0, 10)).toBe(0);
20+
expect(fx.clamp(15, 0, 10)).toBe(10);
21+
});
22+
23+
it('isNumber should identify numbers correctly', () => {
24+
expect(fx.isNumber(123)).toBe(true);
25+
expect(fx.isNumber('123')).toBe(true);
26+
expect(fx.isNumber('abc')).toBe(false);
27+
// Current implementation treats null as 0, hence isNumber(null) is true
28+
expect(fx.isNumber(null)).toBe(true);
29+
expect(fx.isNumber(undefined)).toBe(false);
30+
});
31+
});
32+
33+
describe('Expression Evaluation', () => {
34+
it('evaluateExpression should handle basic arithmetic', () => {
35+
expect(fx.evaluateExpression('1 + 1')).toBe(2);
36+
expect(fx.evaluateExpression('10 * 2')).toBe(20);
37+
expect(fx.evaluateExpression('(2 + 3) * 4')).toBe(20);
38+
});
39+
40+
// Test a more complex case if possible, but keep it simple for now
41+
it('evaluateExpression should respect operator precedence', () => {
42+
expect(fx.evaluateExpression('2 + 3 * 4')).toBe(14);
43+
});
44+
});
45+
});
46+

0 commit comments

Comments
 (0)