Files
dingtalk-workspace-cli/internal/helpers/connect_knowledge_test.go
T
修雨 c151756168 feat(connect): Q&A-bot hardening for the stream bridge
Five operational gaps that bite when a connector-backed bot answers
questions in real groups:

- Single-instance lock: one connector per robot per machine (pid file,
  stale-lock takeover). Duplicate Stream connections on one clientId get
  messages load-balanced between them, so the bot answers intermittently
  (verified live).
- Access policy: --allowed-users / --allowed-groups allowlists plus a
  per-sender rate limit (--user-rate-limit, default 20/min). Every
  message is an LLM call; without this anyone who can reach the bot can
  spend on its behalf.
- Per-conversation FIFO queue: same-chat messages run in arrival order
  (follow-ups need the previous turn's --resume session; parallel CLIs
  racing one session corrupt it); different chats stay parallel.
- Picture messages: resolve the callback downloadCode via robot
  messageFiles/download to a local temp file and hand the path to the
  agent CLI — error screenshots are the top Q&A inbound and were
  previously dropped silently. The claudecode persona now permits
  reading attached files.
- --knowledge-dir: lightweight local retrieval (markdown chunking,
  latin-token + CJK-bigram scoring) prepends top-k matching chunks to
  each question while the agent keeps running from the clean scratch
  dir, so replies stay inside DingTalk's AI-assistant response window.

All knobs have env mirrors (DWS_ALLOWED_USERS / DWS_ALLOWED_GROUPS /
DWS_USER_RATE_LIMIT / DWS_KNOWLEDGE_DIR) for service-style runs.

go build ./... clean; go test ./internal/helpers -race passes with new
tests for lock/gate/queue/knowledge/media. test/cli_compat, test/scripts
and test/unit fail identically on the unmodified base (pre-existing
fixture issues).
2026-06-11 12:18:25 +08:00

114 lines
3.2 KiB
Go

// Copyright 2026 Alibaba Group
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package helpers
import (
"os"
"path/filepath"
"strings"
"testing"
)
func writeKnowledgeDir(t *testing.T) string {
t.Helper()
dir := t.TempDir()
faq := `# 安装
dws 通过 brew install dws 安装,升级用 dws upgrade。
# 缓存问题
出现幽灵命令或命令缺失时,运行 dws cache refresh 强制刷新发现配置缓存。
`
bot := `# 机器人建联
用 dws devapp robot create 建号,dws devapp robot connect 建联。
卡片停在数据加载中说明回复超过了 AI 助理应答窗口。
`
if err := os.WriteFile(filepath.Join(dir, "faq.md"), []byte(faq), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "bot.md"), []byte(bot), 0o644); err != nil {
t.Fatal(err)
}
// Non-knowledge files are ignored.
if err := os.WriteFile(filepath.Join(dir, "logo.png"), []byte{0x89, 0x50}, 0o644); err != nil {
t.Fatal(err)
}
return dir
}
func TestKnowledgeBaseLoadAndChunk(t *testing.T) {
kb, err := loadKnowledgeBase(writeKnowledgeDir(t))
if err != nil {
t.Fatalf("load: %v", err)
}
// faq.md has 2 heading-chunks, bot.md has 1.
if len(kb.chunks) != 3 {
t.Fatalf("chunks = %d, want 3", len(kb.chunks))
}
}
func TestKnowledgeAugmentPicksRelevantChunk(t *testing.T) {
kb, err := loadKnowledgeBase(writeKnowledgeDir(t))
if err != nil {
t.Fatal(err)
}
got := kb.augment("机器人卡在数据加载中怎么办")
if !strings.Contains(got, "AI 助理应答窗口") {
t.Fatalf("augment missed the bot chunk:\n%s", got)
}
if !strings.Contains(got, "用户问题:机器人卡在数据加载中怎么办") {
t.Fatalf("augment must keep the original question:\n%s", got)
}
if !strings.Contains(got, "bot.md") {
t.Fatalf("augment must cite the source file:\n%s", got)
}
}
func TestKnowledgeAugmentEnglishTerms(t *testing.T) {
kb, err := loadKnowledgeBase(writeKnowledgeDir(t))
if err != nil {
t.Fatal(err)
}
got := kb.augment("cache refresh 是干什么的")
if !strings.Contains(got, "强制刷新发现配置缓存") {
t.Fatalf("augment missed the cache chunk:\n%s", got)
}
}
func TestKnowledgeAugmentNoMatchPassesThrough(t *testing.T) {
kb, err := loadKnowledgeBase(writeKnowledgeDir(t))
if err != nil {
t.Fatal(err)
}
q := "qqqq zzzz xxxx"
if got := kb.augment(q); got != q {
t.Fatalf("no-match should pass through, got:\n%s", got)
}
}
func TestKnowledgeBaseEmptyDirErrors(t *testing.T) {
if _, err := loadKnowledgeBase(t.TempDir()); err == nil {
t.Fatal("empty dir should error so a typo'd --knowledge-dir is loud")
}
}
func TestKnowledgeNilBasePassesThrough(t *testing.T) {
var kb *knowledgeBase
if got := kb.augment("你好"); got != "你好" {
t.Fatalf("nil kb should pass through, got %q", got)
}
}