@zhin.js/core 1.0.39 → 1.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,145 +0,0 @@
1
- /**
2
- * Feature 基类测试
3
- */
4
- import { describe, it, expect } from 'vitest';
5
- import { Feature, FeatureJSON } from '../src/feature.js';
6
-
7
- // 创建一个具体的 Feature 子类用于测试
8
- class TestFeature extends Feature<{ id: string; value: number }> {
9
- readonly name = 'test';
10
- readonly icon = 'TestIcon';
11
- readonly desc = '测试 Feature';
12
-
13
- toJSON(pluginName?: string): FeatureJSON {
14
- const list = pluginName ? this.getByPlugin(pluginName) : this.items;
15
- return {
16
- name: this.name,
17
- icon: this.icon,
18
- desc: this.desc,
19
- count: list.length,
20
- items: list.map(item => ({ id: item.id, value: item.value })),
21
- };
22
- }
23
- }
24
-
25
- describe('Feature 基类', () => {
26
- describe('add / remove / getAll', () => {
27
- it('应该能添加 item 并返回 dispose 函数', () => {
28
- const feature = new TestFeature();
29
- const item = { id: 'a', value: 1 };
30
- const dispose = feature.add(item, 'plugin-a');
31
-
32
- expect(feature.items).toHaveLength(1);
33
- expect(feature.items[0]).toBe(item);
34
- expect(typeof dispose).toBe('function');
35
- });
36
-
37
- it('dispose 函数应移除 item', () => {
38
- const feature = new TestFeature();
39
- const item = { id: 'a', value: 1 };
40
- const dispose = feature.add(item, 'plugin-a');
41
-
42
- dispose();
43
- expect(feature.items).toHaveLength(0);
44
- });
45
-
46
- it('remove 应返回 true 并移除 item', () => {
47
- const feature = new TestFeature();
48
- const item = { id: 'a', value: 1 };
49
- feature.add(item, 'plugin-a');
50
-
51
- const result = feature.remove(item);
52
- expect(result).toBe(true);
53
- expect(feature.items).toHaveLength(0);
54
- });
55
-
56
- it('remove 不存在的 item 应返回 false', () => {
57
- const feature = new TestFeature();
58
- const result = feature.remove({ id: 'nonexistent', value: 0 });
59
- expect(result).toBe(false);
60
- });
61
-
62
- it('应支持多个 item', () => {
63
- const feature = new TestFeature();
64
- feature.add({ id: 'a', value: 1 }, 'plugin-a');
65
- feature.add({ id: 'b', value: 2 }, 'plugin-b');
66
- feature.add({ id: 'c', value: 3 }, 'plugin-a');
67
-
68
- expect(feature.items).toHaveLength(3);
69
- expect(feature.count).toBe(3);
70
- });
71
- });
72
-
73
- describe('插件追踪: getByPlugin / countByPlugin', () => {
74
- it('应按插件名分组 items', () => {
75
- const feature = new TestFeature();
76
- feature.add({ id: 'a', value: 1 }, 'plugin-a');
77
- feature.add({ id: 'b', value: 2 }, 'plugin-b');
78
- feature.add({ id: 'c', value: 3 }, 'plugin-a');
79
-
80
- const pluginAItems = feature.getByPlugin('plugin-a');
81
- expect(pluginAItems).toHaveLength(2);
82
- expect(pluginAItems.map(i => i.id)).toEqual(['a', 'c']);
83
-
84
- const pluginBItems = feature.getByPlugin('plugin-b');
85
- expect(pluginBItems).toHaveLength(1);
86
- expect(pluginBItems[0].id).toBe('b');
87
- });
88
-
89
- it('不存在的插件应返回空数组', () => {
90
- const feature = new TestFeature();
91
- expect(feature.getByPlugin('nonexistent')).toEqual([]);
92
- });
93
-
94
- it('countByPlugin 应返回正确数量', () => {
95
- const feature = new TestFeature();
96
- feature.add({ id: 'a', value: 1 }, 'plugin-a');
97
- feature.add({ id: 'b', value: 2 }, 'plugin-a');
98
-
99
- expect(feature.countByPlugin('plugin-a')).toBe(2);
100
- expect(feature.countByPlugin('plugin-b')).toBe(0);
101
- });
102
-
103
- it('remove 应同时从 pluginItems 中移除', () => {
104
- const feature = new TestFeature();
105
- const item = { id: 'a', value: 1 };
106
- feature.add(item, 'plugin-a');
107
- feature.remove(item);
108
-
109
- expect(feature.getByPlugin('plugin-a')).toHaveLength(0);
110
- });
111
- });
112
-
113
- describe('toJSON 序列化', () => {
114
- it('无参数时返回全部 items', () => {
115
- const feature = new TestFeature();
116
- feature.add({ id: 'a', value: 1 }, 'plugin-a');
117
- feature.add({ id: 'b', value: 2 }, 'plugin-b');
118
-
119
- const json = feature.toJSON();
120
- expect(json.name).toBe('test');
121
- expect(json.icon).toBe('TestIcon');
122
- expect(json.desc).toBe('测试 Feature');
123
- expect(json.count).toBe(2);
124
- expect(json.items).toHaveLength(2);
125
- });
126
-
127
- it('传 pluginName 时只返回该插件的 items', () => {
128
- const feature = new TestFeature();
129
- feature.add({ id: 'a', value: 1 }, 'plugin-a');
130
- feature.add({ id: 'b', value: 2 }, 'plugin-b');
131
-
132
- const json = feature.toJSON('plugin-a');
133
- expect(json.count).toBe(1);
134
- expect(json.items).toHaveLength(1);
135
- expect(json.items[0].id).toBe('a');
136
- });
137
- });
138
-
139
- describe('extensions getter', () => {
140
- it('默认返回空对象', () => {
141
- const feature = new TestFeature();
142
- expect(feature.extensions).toEqual({});
143
- });
144
- });
145
- });