签到
This commit is contained in:
@@ -0,0 +1,158 @@
|
|||||||
|
# 签到工具查询功能增强
|
||||||
|
|
||||||
|
## 问题描述
|
||||||
|
|
||||||
|
原签到工具只支持签到操作,不支持查询。当用户询问"今天有多少人签到"或"最近几天的签到情况"时,AI 无法调用工具获取准确数据,导致:
|
||||||
|
|
||||||
|
1. **数据不准确**:AI 只能根据上下文猜测或给出模糊答案
|
||||||
|
2. **无法查询历史**:询问超过一天的数据时,AI 一问三不知
|
||||||
|
3. **功能不完整**:数据库层已有完善的查询能力(`CountSigns`、`CountSignsByDay`),但工具层未暴露给 AI
|
||||||
|
|
||||||
|
## 解决方案
|
||||||
|
|
||||||
|
为签到工具添加查询功能,支持以下两种操作模式:
|
||||||
|
|
||||||
|
### 1. 签到操作 (action=sign)
|
||||||
|
|
||||||
|
保持原有功能不变:
|
||||||
|
- 记录节点今日签到信息
|
||||||
|
- 每个节点每天只能签到一次
|
||||||
|
- 必填参数:地区、名字、设备
|
||||||
|
|
||||||
|
### 2. 查询操作 (action=query)
|
||||||
|
|
||||||
|
新增查询功能:
|
||||||
|
- 查询指定日期或日期范围的签到统计
|
||||||
|
- 返回总人次和按天分组的统计数据
|
||||||
|
- 可选参数:
|
||||||
|
- `date`: 查询日期(格式:YYYY-MM-DD),默认今天
|
||||||
|
- `days`: 查询最近 N 天,默认只查询 date 指定的那一天
|
||||||
|
|
||||||
|
## 修改内容
|
||||||
|
|
||||||
|
### 1. 扩展 SignStore 接口
|
||||||
|
|
||||||
|
```go
|
||||||
|
type SignStore interface {
|
||||||
|
CreateSign(nodeID string, longName, shortName *string, signText string, signTime time.Time) (*storepkg.SignRecord, error)
|
||||||
|
HasSignedOnDay(nodeID string, day time.Time) (bool, error)
|
||||||
|
GetNodeInfo(nodeID string) (*storepkg.NodeInfoRecord, error)
|
||||||
|
// 新增查询方法
|
||||||
|
CountSigns(opts storepkg.ListOptions) (int64, error)
|
||||||
|
CountSignsByDay(opts storepkg.ListOptions) ([]storepkg.SignDayCount, error)
|
||||||
|
ListSigns(opts storepkg.ListOptions) ([]storepkg.SignRecord, error)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 更新工具定义
|
||||||
|
|
||||||
|
工具描述中明确说明支持两种操作:
|
||||||
|
- action=sign:签到
|
||||||
|
- action=query:查询统计
|
||||||
|
|
||||||
|
### 3. 实现查询逻辑
|
||||||
|
|
||||||
|
- `executeSign()`: 原签到逻辑
|
||||||
|
- `executeQuery()`: 新增查询逻辑
|
||||||
|
- 解析日期参数
|
||||||
|
- 构建查询条件(Since/Until)
|
||||||
|
- 调用 store 查询接口
|
||||||
|
- 格式化返回结果
|
||||||
|
|
||||||
|
### 4. 扩展参数结构
|
||||||
|
|
||||||
|
```go
|
||||||
|
type signParams struct {
|
||||||
|
Action string `json:"action"` // sign 或 query
|
||||||
|
// 签到参数
|
||||||
|
Region string `json:"region"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Device string `json:"device"`
|
||||||
|
TxPower string `json:"tx_power"`
|
||||||
|
AntennaLength string `json:"antenna_length"`
|
||||||
|
Altitude string `json:"altitude"`
|
||||||
|
RawText string `json:"raw_text"`
|
||||||
|
// 查询参数
|
||||||
|
Date string `json:"date"` // YYYY-MM-DD
|
||||||
|
Days int `json:"days"` // 最近 N 天
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 使用示例
|
||||||
|
|
||||||
|
### AI 调用示例
|
||||||
|
|
||||||
|
#### 查询今天的签到情况
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"action": "query",
|
||||||
|
"date": "2024-06-23"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
返回:
|
||||||
|
```
|
||||||
|
2024-06-23 的签到统计:
|
||||||
|
总计:5 人次
|
||||||
|
|
||||||
|
按天统计:
|
||||||
|
- 2024-06-23: 5 人
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 查询最近 7 天的签到情况
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"action": "query",
|
||||||
|
"date": "2024-06-23",
|
||||||
|
"days": 7
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
返回:
|
||||||
|
```
|
||||||
|
最近 7 天的签到统计:
|
||||||
|
总计:32 人次
|
||||||
|
|
||||||
|
按天统计:
|
||||||
|
- 2024-06-23: 5 人
|
||||||
|
- 2024-06-22: 6 人
|
||||||
|
- 2024-06-21: 4 人
|
||||||
|
- 2024-06-20: 7 人
|
||||||
|
- 2024-06-19: 3 人
|
||||||
|
- 2024-06-18: 4 人
|
||||||
|
- 2024-06-17: 3 人
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 签到(保持原有功能)
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"action": "sign",
|
||||||
|
"region": "上海闵行",
|
||||||
|
"name": "Kevin",
|
||||||
|
"device": "GAT562"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 测试
|
||||||
|
|
||||||
|
创建了完整的单元测试 `sign_test.go`,覆盖:
|
||||||
|
- ✅ 查询今天的签到统计
|
||||||
|
- ✅ 查询最近 N 天的签到统计
|
||||||
|
- ✅ 查询指定日期的签到统计
|
||||||
|
- ✅ 签到功能(回归测试)
|
||||||
|
|
||||||
|
所有测试通过。
|
||||||
|
|
||||||
|
## 影响范围
|
||||||
|
|
||||||
|
- ✅ 向后兼容:原有签到功能完全保持不变
|
||||||
|
- ✅ 数据库无需修改:查询接口已存在
|
||||||
|
- ✅ 新增功能:AI 现在可以准确回答签到统计问题
|
||||||
|
- ✅ 编译通过:整个项目构建成功
|
||||||
|
|
||||||
|
## 后续建议
|
||||||
|
|
||||||
|
可以考虑进一步增强:
|
||||||
|
1. 支持按节点查询:某个特定节点的签到历史
|
||||||
|
2. 支持按地区统计:哪些地区签到最活跃
|
||||||
|
3. 支持导出详细列表:不仅是统计,还能看到每条签到的详细内容
|
||||||
+119
-18
@@ -26,6 +26,9 @@ type SignStore interface {
|
|||||||
CreateSign(nodeID string, longName, shortName *string, signText string, signTime time.Time) (*storepkg.SignRecord, error)
|
CreateSign(nodeID string, longName, shortName *string, signText string, signTime time.Time) (*storepkg.SignRecord, error)
|
||||||
HasSignedOnDay(nodeID string, day time.Time) (bool, error)
|
HasSignedOnDay(nodeID string, day time.Time) (bool, error)
|
||||||
GetNodeInfo(nodeID string) (*storepkg.NodeInfoRecord, error)
|
GetNodeInfo(nodeID string) (*storepkg.NodeInfoRecord, error)
|
||||||
|
CountSigns(opts storepkg.ListOptions) (int64, error)
|
||||||
|
CountSignsByDay(opts storepkg.ListOptions) ([]storepkg.SignDayCount, error)
|
||||||
|
ListSigns(opts storepkg.ListOptions) ([]storepkg.SignRecord, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tool 是签到工具。
|
// Tool 是签到工具。
|
||||||
@@ -42,8 +45,9 @@ func (t *Tool) Enabled() bool { return t.enabled && t.store != nil }
|
|||||||
|
|
||||||
// ToolDefinition returns the OpenAI tool definition
|
// ToolDefinition returns the OpenAI tool definition
|
||||||
func (t *Tool) ToolDefinition(description string) *model.Tool {
|
func (t *Tool) ToolDefinition(description string) *model.Tool {
|
||||||
desc := "签到工具。当用户想要签到/打卡/上台时调用。会记录该节点今日的签到信息,每个节点每天只能签到一次。" +
|
desc := "签到工具。支持签到和查询两种操作:\n" +
|
||||||
"必填参数:地区、名字、设备;可选参数:发射功率、天线长度、身处高度。"
|
"1. 签到操作(action=sign):记录节点今日签到信息,每个节点每天只能签到一次。必填参数:地区、名字、设备\n" +
|
||||||
|
"2. 查询操作(action=query):查询签到统计。可按日期范围查询,默认查询今天。返回签到总数和按天的统计数据"
|
||||||
if description != "" {
|
if description != "" {
|
||||||
desc = description
|
desc = description
|
||||||
}
|
}
|
||||||
@@ -55,36 +59,49 @@ func (t *Tool) ToolDefinition(description string) *model.Tool {
|
|||||||
Parameters: map[string]any{
|
Parameters: map[string]any{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": map[string]any{
|
"properties": map[string]any{
|
||||||
|
"action": map[string]any{
|
||||||
|
"type": "string",
|
||||||
|
"enum": []string{"sign", "query"},
|
||||||
|
"description": "操作类型:sign=签到,query=查询签到统计",
|
||||||
|
},
|
||||||
"region": map[string]any{
|
"region": map[string]any{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "地区,例如 \"上海闵行\"、\"安徽\"、\"广东深圳\"",
|
"description": "签到时必填:地区,例如 \"上海闵行\"、\"安徽\"、\"广东深圳\"",
|
||||||
},
|
},
|
||||||
"name": map[string]any{
|
"name": map[string]any{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "签到用户的名字/呼号,例如 \"Kevin\"、\"TaoEngine\"",
|
"description": "签到时必填:签到用户的名字/呼号,例如 \"Kevin\"、\"TaoEngine\"",
|
||||||
},
|
},
|
||||||
"device": map[string]any{
|
"device": map[string]any{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "使用的设备型号,例如 \"GAT562\"、\"EBYTE_EoRa_S3\"",
|
"description": "签到时必填:使用的设备型号,例如 \"GAT562\"、\"EBYTE_EoRa_S3\"",
|
||||||
},
|
},
|
||||||
"tx_power": map[string]any{
|
"tx_power": map[string]any{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "可选:发射功率,例如 \"25mW\"、\"100mW\"",
|
"description": "签到时可选:发射功率,例如 \"25mW\"、\"100mW\"",
|
||||||
},
|
},
|
||||||
"antenna_length": map[string]any{
|
"antenna_length": map[string]any{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "可选:天线长度,例如 \"5dBi\"、\"1.2m\"",
|
"description": "签到时可选:天线长度,例如 \"5dBi\"、\"1.2m\"",
|
||||||
},
|
},
|
||||||
"altitude": map[string]any{
|
"altitude": map[string]any{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "可选:身处高度,例如 \"30m\"、\"海拔500m\"",
|
"description": "签到时可选:身处高度,例如 \"30m\"、\"海拔500m\"",
|
||||||
},
|
},
|
||||||
"raw_text": map[string]any{
|
"raw_text": map[string]any{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "可选:用户原始签到文本。当无法准确拆分地区/名字/设备时,传入用户原文作为签到正文",
|
"description": "签到时可选:用户原始签到文本。当无法准确拆分地区/名字/设备时,传入用户原文作为签到正文",
|
||||||
|
},
|
||||||
|
"date": map[string]any{
|
||||||
|
"type": "string",
|
||||||
|
"description": "查询时可选:查询日期,格式 YYYY-MM-DD,例如 \"2024-06-23\"。不填则查询今天",
|
||||||
|
},
|
||||||
|
"days": map[string]any{
|
||||||
|
"type": "integer",
|
||||||
|
"description": "查询时可选:查询最近N天的数据,例如 7 表示最近7天。不填则只查询date指定的那一天",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"required": []string{"region", "name", "device"},
|
"required": []string{"action"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -101,6 +118,19 @@ func (t *Tool) Execute(ctx context.Context, args string, runtime agenttool.Runti
|
|||||||
return "", fmt.Errorf("failed to parse arguments: %w", err)
|
return "", fmt.Errorf("failed to parse arguments: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据 action 参数路由到不同的操作
|
||||||
|
switch strings.ToLower(strings.TrimSpace(params.Action)) {
|
||||||
|
case "query":
|
||||||
|
return t.executeQuery(ctx, params, runtime)
|
||||||
|
case "sign", "":
|
||||||
|
return t.executeSign(ctx, params, runtime)
|
||||||
|
default:
|
||||||
|
return "", fmt.Errorf("无效的操作类型:%s,只支持 sign 或 query", params.Action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// executeSign 执行签到操作
|
||||||
|
func (t *Tool) executeSign(ctx context.Context, params signParams, runtime agenttool.Runtime) (string, error) {
|
||||||
params.Region = strings.TrimSpace(params.Region)
|
params.Region = strings.TrimSpace(params.Region)
|
||||||
params.Name = strings.TrimSpace(params.Name)
|
params.Name = strings.TrimSpace(params.Name)
|
||||||
params.Device = strings.TrimSpace(params.Device)
|
params.Device = strings.TrimSpace(params.Device)
|
||||||
@@ -146,6 +176,75 @@ func (t *Tool) Execute(ctx context.Context, args string, runtime agenttool.Runti
|
|||||||
return fmt.Sprintf("签到成功!%s\n签到内容:%s", displayName(node), record.SignText), nil
|
return fmt.Sprintf("签到成功!%s\n签到内容:%s", displayName(node), record.SignText), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// executeQuery 执行查询操作
|
||||||
|
func (t *Tool) executeQuery(ctx context.Context, params signParams, runtime agenttool.Runtime) (string, error) {
|
||||||
|
now := runtime.Now
|
||||||
|
if now.IsZero() {
|
||||||
|
now = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析查询日期
|
||||||
|
var targetDate time.Time
|
||||||
|
if params.Date != "" {
|
||||||
|
var err error
|
||||||
|
targetDate, err = time.Parse("2006-01-02", params.Date)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("日期格式错误,应为 YYYY-MM-DD:%v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 默认查询今天
|
||||||
|
targetDate = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建查询选项
|
||||||
|
var opts storepkg.ListOptions
|
||||||
|
if params.Days > 0 {
|
||||||
|
// 查询最近N天
|
||||||
|
since := targetDate.AddDate(0, 0, -params.Days+1)
|
||||||
|
until := targetDate.Add(24*time.Hour - time.Nanosecond)
|
||||||
|
opts.Since = &since
|
||||||
|
opts.Until = &until
|
||||||
|
} else {
|
||||||
|
// 只查询指定的那一天
|
||||||
|
since := targetDate
|
||||||
|
until := targetDate.Add(24*time.Hour - time.Nanosecond)
|
||||||
|
opts.Since = &since
|
||||||
|
opts.Until = &until
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取总数
|
||||||
|
total, err := t.store.CountSigns(opts)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("查询签到总数失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取按天统计
|
||||||
|
dayCounts, err := t.store.CountSignsByDay(opts)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("查询按天统计失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建返回消息
|
||||||
|
var result strings.Builder
|
||||||
|
if params.Days > 0 {
|
||||||
|
result.WriteString(fmt.Sprintf("最近 %d 天的签到统计:\n", params.Days))
|
||||||
|
} else {
|
||||||
|
result.WriteString(fmt.Sprintf("%s 的签到统计:\n", targetDate.Format("2006-01-02")))
|
||||||
|
}
|
||||||
|
result.WriteString(fmt.Sprintf("总计:%d 人次\n\n", total))
|
||||||
|
|
||||||
|
if len(dayCounts) > 0 {
|
||||||
|
result.WriteString("按天统计:\n")
|
||||||
|
for _, dc := range dayCounts {
|
||||||
|
result.WriteString(fmt.Sprintf("- %s: %d 人\n", dc.Date, dc.Count))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result.WriteString("该时间段内没有签到记录")
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// resolveNodeNames 取出节点的 long_name / short_name。
|
// resolveNodeNames 取出节点的 long_name / short_name。
|
||||||
// text_message 包本身不含名字,队列记录里的 long_name/short_name 经常为空;
|
// text_message 包本身不含名字,队列记录里的 long_name/short_name 经常为空;
|
||||||
// 此时用 node_id 查 nodeinfo 表补全,保证签到记录里能看到节点名。
|
// 此时用 node_id 查 nodeinfo 表补全,保证签到记录里能看到节点名。
|
||||||
@@ -187,14 +286,16 @@ func resolveNodeNames(t *Tool, node agenttool.NodeContext) (*string, *string) {
|
|||||||
|
|
||||||
// signParams 是签到工具的入参。
|
// signParams 是签到工具的入参。
|
||||||
type signParams struct {
|
type signParams struct {
|
||||||
Region string `json:"region"`
|
Action string `json:"action"` // 操作类型:sign=签到,query=查询
|
||||||
Name string `json:"name"`
|
Region string `json:"region"` // 签到时使用
|
||||||
Device string `json:"device"`
|
Name string `json:"name"` // 签到时使用
|
||||||
TxPower string `json:"tx_power"`
|
Device string `json:"device"` // 签到时使用
|
||||||
AntennaLength string `json:"antenna_length"`
|
TxPower string `json:"tx_power"` // 签到时使用
|
||||||
Altitude string `json:"altitude"`
|
AntennaLength string `json:"antenna_length"` // 签到时使用
|
||||||
// RawText 是用户原始签到文本,结构化字段缺失时作为签到正文回退使用。
|
Altitude string `json:"altitude"` // 签到时使用
|
||||||
RawText string `json:"raw_text"`
|
RawText string `json:"raw_text"` // 签到时使用:用户原始签到文本
|
||||||
|
Date string `json:"date"` // 查询时使用:日期 YYYY-MM-DD
|
||||||
|
Days int `json:"days"` // 查询时使用:查询最近N天
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildSignText 按参考格式拼装签到正文:地区-名字-设备签到,可选信息附在括号内。
|
// buildSignText 按参考格式拼装签到正文:地区-名字-设备签到,可选信息附在括号内。
|
||||||
|
|||||||
@@ -0,0 +1,220 @@
|
|||||||
|
package sign
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"meshtastic_mqtt_server/internal/agenttool"
|
||||||
|
storepkg "meshtastic_mqtt_server/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// mockSignStore 是用于测试的 mock store
|
||||||
|
type mockSignStore struct {
|
||||||
|
signs []storepkg.SignRecord
|
||||||
|
nodeInfoMap map[string]*storepkg.NodeInfoRecord
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockSignStore) CreateSign(nodeID string, longName, shortName *string, signText string, signTime time.Time) (*storepkg.SignRecord, error) {
|
||||||
|
record := storepkg.SignRecord{
|
||||||
|
NodeID: nodeID,
|
||||||
|
LongName: longName,
|
||||||
|
ShortName: shortName,
|
||||||
|
SignText: signText,
|
||||||
|
SignTime: signTime,
|
||||||
|
}
|
||||||
|
m.signs = append(m.signs, record)
|
||||||
|
return &record, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockSignStore) HasSignedOnDay(nodeID string, day time.Time) (bool, error) {
|
||||||
|
loc := day.Location()
|
||||||
|
if loc == nil {
|
||||||
|
loc = time.Local
|
||||||
|
}
|
||||||
|
start := time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, loc)
|
||||||
|
end := start.AddDate(0, 0, 1)
|
||||||
|
|
||||||
|
for _, sign := range m.signs {
|
||||||
|
if sign.NodeID == nodeID && sign.SignTime.After(start) && sign.SignTime.Before(end) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockSignStore) GetNodeInfo(nodeID string) (*storepkg.NodeInfoRecord, error) {
|
||||||
|
return m.nodeInfoMap[nodeID], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockSignStore) CountSigns(opts storepkg.ListOptions) (int64, error) {
|
||||||
|
count := int64(0)
|
||||||
|
for _, sign := range m.signs {
|
||||||
|
if opts.Since != nil && sign.SignTime.Before(*opts.Since) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if opts.Until != nil && sign.SignTime.After(*opts.Until) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockSignStore) CountSignsByDay(opts storepkg.ListOptions) ([]storepkg.SignDayCount, error) {
|
||||||
|
dayCounts := make(map[string]int64)
|
||||||
|
for _, sign := range m.signs {
|
||||||
|
if opts.Since != nil && sign.SignTime.Before(*opts.Since) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if opts.Until != nil && sign.SignTime.After(*opts.Until) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dateStr := sign.SignTime.Format("2006-01-02")
|
||||||
|
dayCounts[dateStr]++
|
||||||
|
}
|
||||||
|
|
||||||
|
var result []storepkg.SignDayCount
|
||||||
|
for date, count := range dayCounts {
|
||||||
|
result = append(result, storepkg.SignDayCount{Date: date, Count: count})
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockSignStore) ListSigns(opts storepkg.ListOptions) ([]storepkg.SignRecord, error) {
|
||||||
|
var result []storepkg.SignRecord
|
||||||
|
for _, sign := range m.signs {
|
||||||
|
if opts.Since != nil && sign.SignTime.Before(*opts.Since) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if opts.Until != nil && sign.SignTime.After(*opts.Until) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, sign)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSignTool_Query(t *testing.T) {
|
||||||
|
// 创建 mock store 并添加测试数据
|
||||||
|
now := time.Date(2024, 6, 23, 12, 0, 0, 0, time.UTC)
|
||||||
|
yesterday := now.AddDate(0, 0, -1)
|
||||||
|
twoDaysAgo := now.AddDate(0, 0, -2)
|
||||||
|
|
||||||
|
store := &mockSignStore{
|
||||||
|
signs: []storepkg.SignRecord{
|
||||||
|
{NodeID: "node1", SignText: "上海-Alice-Device1签到", SignTime: now},
|
||||||
|
{NodeID: "node2", SignText: "北京-Bob-Device2签到", SignTime: now},
|
||||||
|
{NodeID: "node3", SignText: "深圳-Charlie-Device3签到", SignTime: yesterday},
|
||||||
|
{NodeID: "node4", SignText: "广州-David-Device4签到", SignTime: twoDaysAgo},
|
||||||
|
},
|
||||||
|
nodeInfoMap: make(map[string]*storepkg.NodeInfoRecord),
|
||||||
|
}
|
||||||
|
|
||||||
|
tool := &Tool{
|
||||||
|
enabled: true,
|
||||||
|
store: store,
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
action string
|
||||||
|
date string
|
||||||
|
days int
|
||||||
|
expectCount int64
|
||||||
|
expectError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "查询今天",
|
||||||
|
action: "query",
|
||||||
|
date: "2024-06-23",
|
||||||
|
days: 0,
|
||||||
|
expectCount: 2,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "查询最近3天",
|
||||||
|
action: "query",
|
||||||
|
date: "2024-06-23",
|
||||||
|
days: 3,
|
||||||
|
expectCount: 4,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "查询昨天",
|
||||||
|
action: "query",
|
||||||
|
date: "2024-06-22",
|
||||||
|
days: 0,
|
||||||
|
expectCount: 1,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
params := signParams{
|
||||||
|
Action: tt.action,
|
||||||
|
Date: tt.date,
|
||||||
|
Days: tt.days,
|
||||||
|
}
|
||||||
|
argsJSON, _ := json.Marshal(params)
|
||||||
|
|
||||||
|
runtime := agenttool.Runtime{Now: now}
|
||||||
|
result, err := tool.Execute(context.Background(), string(argsJSON), runtime)
|
||||||
|
|
||||||
|
if tt.expectError && err == nil {
|
||||||
|
t.Errorf("Expected error but got none")
|
||||||
|
}
|
||||||
|
if !tt.expectError && err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if !tt.expectError {
|
||||||
|
t.Logf("Query result:\n%s", result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSignTool_SignAction(t *testing.T) {
|
||||||
|
now := time.Date(2024, 6, 23, 12, 0, 0, 0, time.UTC)
|
||||||
|
store := &mockSignStore{
|
||||||
|
signs: []storepkg.SignRecord{},
|
||||||
|
nodeInfoMap: make(map[string]*storepkg.NodeInfoRecord),
|
||||||
|
}
|
||||||
|
|
||||||
|
tool := &Tool{
|
||||||
|
enabled: true,
|
||||||
|
store: store,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试签到功能
|
||||||
|
params := signParams{
|
||||||
|
Action: "sign",
|
||||||
|
Region: "上海闵行",
|
||||||
|
Name: "TestUser",
|
||||||
|
Device: "TestDevice",
|
||||||
|
}
|
||||||
|
argsJSON, _ := json.Marshal(params)
|
||||||
|
|
||||||
|
// 创建带节点上下文的 context
|
||||||
|
nodeCtx := agenttool.NodeContext{
|
||||||
|
NodeID: "test_node_123",
|
||||||
|
LongName: "Test Node",
|
||||||
|
ShortName: "TN",
|
||||||
|
}
|
||||||
|
ctx := agenttool.WithNodeContext(context.Background(), nodeCtx)
|
||||||
|
|
||||||
|
runtime := agenttool.Runtime{Now: now}
|
||||||
|
result, err := tool.Execute(ctx, string(argsJSON), runtime)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
t.Logf("Sign result: %s", result)
|
||||||
|
|
||||||
|
// 验证签到记录已创建
|
||||||
|
if len(store.signs) != 1 {
|
||||||
|
t.Errorf("Expected 1 sign record, got %d", len(store.signs))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user