summer-lsp 0.5.0

Language Server Protocol implementation for summer-rs framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# Schema 加载功能测试指南

本文档说明如何测试 summer-lsp 从 target 目录加载 Schema 的功能。

## 单元测试

### 运行所有测试

```bash
cd summer-lsp
cargo test
```

### 运行 Schema 相关测试

```bash
cargo test test_find_schema_in_target --lib
```

### 测试覆盖的场景

1. **test_find_schema_in_target** - 基本的 Schema 查找功能
2. **test_find_schema_in_target_multiple_profiles** - 多个 profile 时选择最新的
3. **test_find_schema_in_target_not_exists** - target 目录不存在的情况
4. **test_load_local_schema_file** - 从文件加载 Schema

## 集成测试示例

运行集成测试示例:

```bash
cargo run --example test_schema_loading
```

这个示例会:
1. 创建临时工作空间
2. 模拟 build.rs 生成的 Schema 文件结构
3. 测试查找和加载功能
4. 验证 Schema 内容

## 端到端测试

### 准备测试项目

1. 创建一个测试 summer-rs 项目:

```bash
cargo new test-spring-app
cd test-spring-app
```

2. 添加依赖到 `Cargo.toml`
```toml
[dependencies]
spring = "0.2"
serde = { version = "1.0", features = ["derive"] }

[build-dependencies]
spring = "0.2"
```

3. 创建配置结构 `src/config.rs`
```rust
use spring::config::Configurable;
use spring::submit_config_schema;
use serde::Deserialize;

#[derive(Debug, Configurable, Deserialize)]
#[config_prefix = "my-service"]
pub struct MyServiceConfig {
    /// Service endpoint URL
    pub endpoint: String,
    
    /// Connection timeout in seconds
    #[serde(default = "default_timeout")]
    pub timeout: u64,
    
    /// Enable retry on failure
    #[serde(default)]
    pub enable_retry: bool,
}

fn default_timeout() -> u64 {
    30
}

// 注册配置 Schema
submit_config_schema!("my-service", MyServiceConfig);
```

4. 创建 `build.rs`
```rust
use spring::config::write_merged_schema_to_file;
use std::env;

fn main() {
    // 生成到 target 目录
    let out_dir = env::var("OUT_DIR").unwrap();
    let schema_path = format!("{}/summer-lsp.schema.json", out_dir);
    
    write_merged_schema_to_file(&schema_path)
        .expect("Failed to write schema file");
    
    println!("cargo:rerun-if-changed=src/config.rs");
    
    // 打印 Schema 路径用于调试
    println!("cargo:warning=Schema generated at: {}", schema_path);
}
```

5. `src/lib.rs` 中引入配置:

```rust
pub mod config;
```

### 编译并验证

1. 编译项目:

```bash
cargo build
```

你应该看到类似的输出:
```
warning: Schema generated at: /path/to/test-spring-app/target/debug/build/test-spring-app-xxx/out/summer-lsp.schema.json
```

2. 验证 Schema 文件存在:

```bash
find target -name "summer-lsp.schema.json"
```

应该输出类似:
```
target/debug/build/test-spring-app-xxx/out/summer-lsp.schema.json
```

3. 查看 Schema 内容:

```bash
cat $(find target -name "summer-lsp.schema.json" | head -1) | jq .
```

应该看到:
```json
{
  "properties": {
    "my-service": {
      "properties": {
        "endpoint": {
          "description": "Service endpoint URL",
          "type": "string"
        },
        "timeout": {
          "default": 30,
          "description": "Connection timeout in seconds",
          "type": "integer"
        },
        "enable-retry": {
          "default": false,
          "description": "Enable retry on failure",
          "type": "boolean"
        }
      },
      "required": ["endpoint"],
      "type": "object"
    }
  },
  "type": "object"
}
```

### 测试 LSP 加载

1. 启动 summer-lsp 服务器(在 summer-lsp 目录):

```bash
cd /path/to/summer-lsp
cargo run
```

2. 在另一个终端,使用 LSP 客户端连接并测试(或使用 VSCode 扩展)

3. 创建配置文件 `config/app.toml`
```toml
[my-service]
endpoint = "https://api.example.com"
timeout = 60
enable-retry = true
```

4. 在编辑器中打开 `config/app.toml`,应该看到:
   - ✅ 智能补全提示 `endpoint``timeout``enable-retry`
   - ✅ 悬停显示字段描述
   - ✅ 类型验证(例如 timeout 必须是数字)
   - ✅ 必填字段验证(endpoint 是必需的)

### 测试多 Profile 场景

1. 编译 debug 版本:

```bash
cargo build
```

2. 编译 release 版本:

```bash
cargo build --release
```

3. 检查两个 Schema 文件:

```bash
find target -name "summer-lsp.schema.json"
```

应该看到两个文件:
```
target/debug/build/test-spring-app-xxx/out/summer-lsp.schema.json
target/release/build/test-spring-app-xxx/out/summer-lsp.schema.json
```

4. LSP 应该加载最新的那个(通过文件修改时间判断)

### 测试配置变化时自动更新

1. 修改 `src/config.rs`,添加新字段:

```rust
#[derive(Debug, Configurable, Deserialize)]
#[config_prefix = "my-service"]
pub struct MyServiceConfig {
    pub endpoint: String,
    pub timeout: u64,
    pub enable_retry: bool,
    
    /// 新增字段:最大重试次数
    #[serde(default = "default_max_retries")]
    pub max_retries: u32,
}

fn default_max_retries() -> u32 {
    3
}
```

2. 重新编译:

```bash
cargo build
```

3. 验证 Schema 已更新:

```bash
cat $(find target -name "summer-lsp.schema.json" | head -1) | jq '.properties."my-service".properties | keys'
```

应该看到新字段:
```json
[
  "enable-retry",
  "endpoint",
  "max-retries",
  "timeout"
]
```

4. 重启 LSP 服务器,在编辑器中应该能看到新字段的补全提示

## 故障排查

### Schema 文件未生成

**检查:**
```bash
cargo clean
cargo build --verbose
```

查看是否有 `cargo:warning=Schema generated at:` 输出。

**可能原因:**
- build.rs 未正确配置
- spring 依赖未添加到 build-dependencies
- submit_config_schema! 未调用

### LSP 未加载 Schema

**检查 LSP 日志:**
```bash
RUST_LOG=summer_lsp=debug cargo run
```

查找类似的日志:
```
[INFO] Loading schema from target directory: ...
[INFO] Loaded 1 local schemas from target
```

**可能原因:**
- target 目录不存在(未编译过)
- Schema 文件路径不正确
- LSP 工作空间路径配置错误

### Schema 内容不正确

**验证 Schema 格式:**
```bash
cat $(find target -name "summer-lsp.schema.json" | head -1) | jq .
```

**检查:**
- 是否有 `properties` 字段
- 配置前缀是否正确
- 字段类型是否正确

## 性能测试

### 测试大型项目

创建一个包含多个配置的项目:

```rust
// 定义 10 个配置结构
#[derive(Configurable, Deserialize)]
#[config_prefix = "service-1"]
pub struct Service1Config { /* ... */ }

submit_config_schema!("service-1", Service1Config);

// ... 重复 10 次
```

编译并测量时间:

```bash
time cargo build
```

检查 Schema 文件大小:

```bash
ls -lh $(find target -name "summer-lsp.schema.json" | head -1)
```

### 测试 LSP 启动时间

```bash
time cargo run -- --version
```

## 自动化测试

### CI/CD 集成

在 `.github/workflows/test.yml` 中添加:

```yaml
name: Test Schema Loading

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
      
      - name: Run unit tests
        run: cargo test test_find_schema_in_target --lib
        working-directory: summer-lsp
      
      - name: Run integration test
        run: cargo run --example test_schema_loading
        working-directory: summer-lsp
      
      - name: Build test project
        run: |
          cargo new test-app
          cd test-app
          # 添加配置和 build.rs
          cargo build
          # 验证 Schema 文件存在
          find target -name "summer-lsp.schema.json" | grep -q .
```

## 总结

测试 Schema 加载功能的完整流程:

1. ✅ 运行单元测试:`cargo test test_find_schema_in_target`
2. ✅ 运行集成测试:`cargo run --example test_schema_loading`
3. ✅ 创建测试项目并验证 Schema 生成
4. ✅ 启动 LSP 并验证 Schema 加载
5. ✅ 在编辑器中测试智能补全和验证功能

所有测试通过即表示功能正常工作!