Files
HomeAgent/internal/plugin/lua_surface_test.go
JianFeeeee 6d38a164eb chore(module): Go module 路径随托管地迁移 gitcode → github
## 背景

仓库早已迁移到 GitHub(`github.com/JianFeeeee/HomeAgent`,CI/CD 都在那边跑),
gitcode 保留为**国内只读镜像**(`ci.yml` 的 sync-gitcode-main 快进推送)。
但 go.mod 的 module 路径一直是 `gitcode.com/JianFeeeee/...` —— 既与权威源
不符,`meta.SourceURL`(AGPL §13 的源码提供入口)也指向镜像而非实际运行的代码。

## 关键区分(这是本次改动安全的前提)

两个 module 是**不同**的,影响面差一个数量级:

| module | 谁依赖 | 改它的后果 |
|---|---|---|
| `.../HomeAgent`(主仓) | **只有主仓自己** | 内部重构,零外部影响 |
| `.../homeagent-sdk`(SDK 仓) | 主仓 49 文件 + 所有外部插件 | 需重编插件 |

★ 实测确认:**插件不 import 主仓 module**(`strings qq/plugin.bin | grep HomeAgent` = 0),
只依赖 SDK 仓。而插件与内核的边界是 **RPC 方法名字符串**
(`plugin.init` / `tool.invoke`…,见 proc/protocol.go),**与 Go module 名无关**。
⇒ module 改名**不改变任何 wire 协议**,现网已装的插件照常运行。

SDK 的 GitHub 仓名是 `homeagentsdk`(无连字符),module 路径必须与之精确匹配,
否则 `go get` 会因路径不符失败,故取 `github.com/JianFeeeee/homeagentsdk`。

## 改动

- `go.mod` / `go.sum`:两个 module 路径 + require/replace
- 277 个 Go 文件 + Makefile + 打包脚本的 import 与 ldflags 路径
- SDK 仓 `meta.CoreModule`、`tools/hmapdev` 的 module 声明与工程模板
- 对外文档/链接的仓库地址(README、PLUGIN_DEV、OVERVIEW 等)

## 明确**保留** gitcode 的地方(镜像机制,不能动)

- `GITCODE_TOKEN`、`GC_URL="…@gitcode.com"`(CI 同步 job)
- `gitcode.com/api/v5/...`(release 附件上传)
- git remote `gitcode`(镜像拉取/推送)
- `docs/zh/ci-cd-runbook.md`(整篇讲镜像运维)

## 验证

- `go build ./...` / `go vet ./...` 绿
- `go test ./... -count=1` → 48 包 ok / 0 FAIL
- hmapdev 工具链编译通过(`third_party/homeagent-sdk/tools/hmapdev`)
- 全仓残留检查:module 形态的 `gitcode.com/JianFeeeee` = 0
- 镜像机制抽验:GC_URL / @gitcode / api/v5 / remote 均完好

## 顺带修正

`hmapdev/templates.go` 的注释此前写「gitcode 的模块不在 proxy.golang.org 上」——
module 已迁 GitHub,该论断不再成立。改为如实说明**为何仍默认 replace 到本机 SDK**:
离线可构建、避免版本漂移、内网可用。理由与托管地无关,故 replace 保留。
2026-10-08 10:40:17 +08:00

189 lines
5.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package plugin
import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
luaSDK "github.com/JianFeeeee/HomeAgent/internal/lua/sdk"
sdk "github.com/JianFeeeee/HomeAgent/internal/sdk"
lua "github.com/yuin/gopher-lua"
)
// TestLuaSDKMockSingleSource 守住「Lua mock 只有一份事实源」。
//
// 三份 sdk.lua(内核内嵌 / 工具链模板 / 项目副本)历史上各自漂移过,
// 表现为「mock 里有的 API,内核运行时是 nil」这类静默失配。
// 事实源是 SDK 仓的 sdk/lua/sdk.lua,内核副本由
// third_party/homeagent-sdk/scripts/sync-lua-sdk.sh 同步。
func TestLuaSDKMockSingleSource(t *testing.T) {
canonical, err := os.ReadFile(filepath.Join("..", "..", "third_party", "homeagent-sdk", "sdk", "lua", "sdk.lua"))
if err != nil {
t.Skipf("SDK repo canonical sdk.lua not available: %v", err)
}
if string(canonical) != luaSDK.SDKSource {
t.Fatal("内核内嵌 sdk.lua 与 SDK 仓 sdk/lua/sdk.lua 不一致;" +
"请跑 third_party/homeagent-sdk/scripts/sync-lua-sdk.sh")
}
}
var luaMockFuncRe = regexp.MustCompile(`(?m)^function sdk\.([A-Za-z0-9_.]+)\s*\(`)
// TestLuaBridgeCoversMock 守住「mock 承诺的每个函数,运行时都有绑定」。
//
// 只查 mock → 运行时这一向:mock 定义了但没绑定,插件会先看到 mock 能调、
// 之后内核里是 nil(或反向的假象)。反向(运行时多出未文档化的函数)无害。
func TestLuaBridgeCoversMock(t *testing.T) {
bridge, err := os.ReadFile("lua_plugin.go")
if err != nil {
t.Fatalf("read lua_plugin.go: %v", err)
}
src := string(bridge)
// 纯 Lua 实现,不经内核绑定。
exempt := map[string]bool{"json.encode": true, "json.decode": true}
matches := luaMockFuncRe.FindAllStringSubmatch(luaSDK.SDKSource, -1)
if len(matches) < 40 {
t.Fatalf("parsed only %d sdk.* functions from mock; parser likely broken", len(matches))
}
for _, m := range matches {
full := m[1]
if exempt[full] {
continue
}
leaf := full
if i := strings.LastIndex(full, "."); i >= 0 {
leaf = full[i+1:]
}
if !strings.Contains(src, `RawSetString("`+leaf+`"`) {
t.Errorf("sdk.%s: mock 有定义,但 lua_plugin.go 没有 RawSetString(%q) 绑定", full, leaf)
}
}
}
func luaTableFrom(t *testing.T, code string) (*lua.LState, *lua.LTable) {
t.Helper()
L := lua.NewState()
if err := L.DoString("return " + code); err != nil {
L.Close()
t.Fatalf("eval lua table: %v", err)
}
tbl, ok := L.Get(-1).(*lua.LTable)
if !ok {
L.Close()
t.Fatalf("expected a table from %q", code)
}
L.Pop(1)
return L, tbl
}
func TestLuaParseInjectOptions(t *testing.T) {
L, tbl := luaTableFrom(t, `{
no_memory = true,
context_policy = "prune",
cleaner_name = "sanitize",
priority = "L2",
}`)
defer L.Close()
L.Push(tbl)
got := parseInjectOptions(L, 1)
L.Pop(1)
if !got.NoMemory {
t.Error("NoMemory should be true")
}
if got.ContextPolicy != sdk.ContextPolicyPrune {
t.Errorf("ContextPolicy = %q, want prune", got.ContextPolicy)
}
if got.CleanerName != "sanitize" {
t.Errorf("CleanerName = %q, want sanitize", got.CleanerName)
}
if got.Priority != sdk.PriorityL2 {
t.Errorf("Priority = %q, want L2", got.Priority)
}
// 缺省/非表 = 零值(记入记忆 + 不裁剪),与旧三参数注入等价。
if z := parseInjectOptions(L, 99); z != (sdk.InjectOptions{}) {
t.Errorf("missing opts should be zero value, got %#v", z)
}
}
func TestLuaContentBlocksParse(t *testing.T) {
L, tbl := luaTableFrom(t, `{
{ type = "text", text = "看图" },
{ type = "image_url", image_url = { url = "data:image/png;base64,AAAA", detail = "high" } },
{ type = "audio_url", audio_url = { url = "https://x/a.mp3" } },
}`)
defer L.Close()
L.Push(tbl)
blocks := luaToContentBlocks(L, 1)
L.Pop(1)
if len(blocks) != 3 {
t.Fatalf("got %d blocks, want 3", len(blocks))
}
if blocks[0].Type != "text" || blocks[0].Text != "看图" {
t.Errorf("block[0] = %#v", blocks[0])
}
if blocks[1].ImageURL == nil || blocks[1].ImageURL.URL != "data:image/png;base64,AAAA" || blocks[1].ImageURL.Detail != "high" {
t.Errorf("block[1] image_url = %#v", blocks[1].ImageURL)
}
if blocks[2].AudioURL == nil || blocks[2].AudioURL.URL != "https://x/a.mp3" {
t.Errorf("block[2] audio_url = %#v", blocks[2].AudioURL)
}
}
func TestLuaAttachmentsBase64(t *testing.T) {
// "hello" 的 base64 是 aGVsbG8=
L, tbl := luaTableFrom(t, `{
{ digest = "sha256:abc", mime = "image/png", name = "a.png" },
{ mime = "image/jpeg", data = "aGVsbG8=" },
{ mime = "image/png", data = "!!!not-base64!!!" },
}`)
defer L.Close()
atts := luaToAttachments(L, tbl)
if len(atts) != 3 {
t.Fatalf("got %d attachments, want 3", len(atts))
}
if atts[0].Digest != "sha256:abc" || atts[0].MIME != "image/png" || atts[0].Name != "a.png" || atts[0].Data != nil {
t.Errorf("att[0] = %#v", atts[0])
}
if string(atts[1].Data) != "hello" {
t.Errorf("att[1] data = %q, want hello", string(atts[1].Data))
}
// 坏 base64 只丢 data,不整单失败——坏附件不应阻断记忆写入。
if atts[2].Data != nil {
t.Errorf("att[2] bad base64 should be dropped, got %q", string(atts[2].Data))
}
}
func TestLuaDefinitionsCarryContextPolicy(t *testing.T) {
L, tbl := luaTableFrom(t, `{
description = "t",
no_memory = true,
context_policy = "prune",
}`)
defer L.Close()
plg := &luaPlugin{name: "cp"}
def := parseToolDef(L, tbl, plg, "t")
if def.ContextPolicy != sdk.ContextPolicyPrune {
t.Errorf("ToolDef.ContextPolicy = %q, want prune", def.ContextPolicy)
}
if !def.NoMemory {
t.Error("ToolDef.NoMemory should be true")
}
chDef := parseChannelDef(L, tbl, plg)
if chDef.ContextPolicy != sdk.ContextPolicyPrune {
t.Errorf("ChannelDef.ContextPolicy = %q, want prune", chDef.ContextPolicy)
}
}