Files
knowledge-governance/cmd/worker/main_test.go
T
姚章浩 ce2a155ebc feat(settings): 知识库绑定按环境一次保存连接与内外库
原「知识库映射 → WeKnora 连接」把地址、文件和绑定分散在多处,页面无法输入密钥,
也看不到候选实例真实可读的库。现在测试 090 / 生产 090 各自一个工作区:草稿填地址
与 API Key,服务器真实分页列库并区分空库/无权限/不可达,选内外库后一次保存连接与
绑定,回读真实生效值。

服务端以修订号 + 行锁 + 单事务写连接、绑定与待入库重排;旧部署无新行时继续使用文件
凭据/默认绑定。密钥只存 AES-256-GCM 密文,主密钥在独立文件;换地址必须重录,旧密钥
不转发、不跨环境取用。消费端(readiness/上传轮询/列库/探活/生产列库)统一从该存储
解析当前连接,避免只保存了页面数据。

验证:go test ./...;前端 606 项;隔离 PostgreSQL 集成用例覆盖保存/冲突/重排/拒错;
make verify 在本提交上执行(结果见交付说明)。
2026-11-19 14:51:47 +08:00

93 lines
3.1 KiB
Go

package main
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"knowledge-governance/internal/appconfig"
postgresstore "knowledge-governance/internal/platform/postgres"
)
func TestRunRequiresDatabaseURL(t *testing.T) {
err := run(context.Background(), func(string) string { return "" })
if err == nil || !strings.Contains(err.Error(), "DATABASE_URL") {
t.Fatalf("run() error = %v, want DATABASE_URL error", err)
}
}
func TestWorkerHandlersExcludeReleaseAndTestJobsByDefault(t *testing.T) {
handlers, err := workerHandlers(nil, appconfig.Config{}, nil)
if err != nil {
t.Fatalf("workerHandlers() error = %v", err)
}
if handlers["demo"] == nil || handlers["demo.sleep"] == nil {
t.Fatalf("worker handlers = %#v, want demo and demo.sleep", handlers)
}
if handlers["release.probe"] != nil {
t.Fatal("ordinary worker must not register release.probe")
}
if handlers["test.upload"] != nil || handlers["test.poll"] != nil || handlers["test.validate"] != nil || handlers["test.remove"] != nil {
t.Fatal("ordinary worker must not register test WeKnora jobs when disabled")
}
if handlers["source.scan.pp"] != nil {
t.Fatal("ordinary worker must not register PP scan jobs when disabled")
}
}
func TestWorkerHandlersRegisterPPScanWhenExplicitlyEnabled(t *testing.T) {
root := t.TempDir()
config := appconfig.Config{
PPScanEnabled: true,
PPScanAllowedRoot: root,
PPScanLedgerPath: filepath.Join(root, "state.jsonl"),
PPScanManifestPath: filepath.Join(root, "manifest.json"),
}
handlers, err := workerHandlers(postgresstore.NewStore(nil), config, nil)
if err != nil {
t.Fatalf("workerHandlers() error = %v", err)
}
if handlers["source.scan.pp"] == nil {
t.Fatal("source.scan.pp handler is not registered")
}
}
func TestWorkerHandlersRequireAPIKeyWhenTestWeKnoraEnabled(t *testing.T) {
config := appconfig.Config{TestWeKnoraEnabled: true, TestWeKnoraAPIKeyFile: "/nonexistent/api-key"}
_, err := workerHandlers(nil, config, nil)
if err == nil || !strings.Contains(err.Error(), "read test WeKnora API key") {
t.Fatalf("workerHandlers() error = %v", err)
}
}
func TestWorkerHandlersRegisterTestJobsForExplicitDockerHTTP(t *testing.T) {
keyFile := filepath.Join(t.TempDir(), "api-key")
if err := os.WriteFile(keyFile, []byte("local-test-key\n"), 0o600); err != nil {
t.Fatal(err)
}
config := appconfig.Config{
TestWeKnoraEnabled: true,
TestWeKnoraAllowDockerHTTP: true,
TestWeKnoraBaseURL: "http://app:8080",
TestWeKnoraAPIKeyFile: keyFile,
GovernanceSpoolRoot: t.TempDir(),
TestWeKnoraHTTPTimeout: time.Second,
TestWeKnoraPollInitial: time.Second,
TestWeKnoraStalledAfter: time.Minute,
TestWeKnoraMaxUploadBytes: 1024,
TestAcceptancePolicyVersion: "structural-v1",
}
handlers, err := workerHandlers(postgresstore.NewStore(nil), config, nil)
if err != nil {
t.Fatalf("workerHandlers() error = %v", err)
}
for _, jobType := range []string{"test.upload", "test.poll", "test.validate", "test.remove"} {
if handlers[jobType] == nil {
t.Fatalf("handler %q is not registered", jobType)
}
}
}