mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-12 18:16:09 +00:00
When a user pastes a Lark or Feishu docx URL in chat, the channel
now detects the URL and fetches the document's raw text via the
Lark Docs API, injecting the content into the agent input inline
so the model can reason over the linked doc without a tool call.
- New Channel.resolveLarkDocs pipeline step runs before reply
context fetch in handleMessageEvent (step 7a)
- LarkClient.GetDocRawContent calls /open-apis/docx/v1/documents
/{id}/raw_content with the existing tenant access token;
permission and not-found errors map to ErrDocAccessDenied
- Per-channel LRU cache (128 entries, 5 min TTL) dedupes repeat
URL references within the window; soft failures are NOT cached
so permission grants become visible immediately
- Rune-safe content truncation at 8000 runes handles CJK docs
without splitting mid-rune
- Bounded concurrency (max 3 parallel fetches) and a per-message
cap of 10 doc URLs act as spam guards
- Tight URL regex anchors the hostname class so a lazy match
cannot bypass via query-string embedding
- 18 new unit tests cover URL extraction edge cases, cache LRU
and TTL semantics, Lark API error code mapping, resolver
end-to-end with nil cache, access denied soft failure,
per-message cap, and UTF-8 truncation
Required Lark app permission: docx:document:readonly, plus
per-document access grant from the doc owner.
Partial fix for #818 (Phase 2 of 3 — thread reply + writer
commands are tracked separately).
79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
package feishu
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDocCache_SetAndGet(t *testing.T) {
|
|
cache := newDocCache(4, time.Minute)
|
|
cache.Set("A", "alpha")
|
|
cache.Set("B", "bravo")
|
|
|
|
if got, ok := cache.Get("A"); !ok || got != "alpha" {
|
|
t.Errorf("Get A: got (%q, %v), want (\"alpha\", true)", got, ok)
|
|
}
|
|
if got, ok := cache.Get("B"); !ok || got != "bravo" {
|
|
t.Errorf("Get B: got (%q, %v), want (\"bravo\", true)", got, ok)
|
|
}
|
|
if _, ok := cache.Get("missing"); ok {
|
|
t.Errorf("Get missing: expected not-found")
|
|
}
|
|
}
|
|
|
|
func TestDocCache_EvictionOnSizeExceeded(t *testing.T) {
|
|
cache := newDocCache(2, time.Minute)
|
|
cache.Set("A", "alpha")
|
|
cache.Set("B", "bravo")
|
|
cache.Set("C", "charlie") // should evict LRU (A)
|
|
|
|
if _, ok := cache.Get("A"); ok {
|
|
t.Errorf("A should be evicted")
|
|
}
|
|
if _, ok := cache.Get("B"); !ok {
|
|
t.Errorf("B should still be present")
|
|
}
|
|
if _, ok := cache.Get("C"); !ok {
|
|
t.Errorf("C should be present")
|
|
}
|
|
}
|
|
|
|
func TestDocCache_LRURecency(t *testing.T) {
|
|
cache := newDocCache(2, time.Minute)
|
|
cache.Set("A", "alpha")
|
|
cache.Set("B", "bravo")
|
|
// Touch A — makes B the LRU.
|
|
if _, ok := cache.Get("A"); !ok {
|
|
t.Fatal("A should be present before touch")
|
|
}
|
|
cache.Set("C", "charlie") // should evict B, not A
|
|
|
|
if _, ok := cache.Get("B"); ok {
|
|
t.Errorf("B should be evicted as LRU")
|
|
}
|
|
if _, ok := cache.Get("A"); !ok {
|
|
t.Errorf("A should still be present (was recently touched)")
|
|
}
|
|
}
|
|
|
|
func TestDocCache_TTLExpiry(t *testing.T) {
|
|
// 50ms TTL + 100ms sleep is loose enough to survive CI scheduler jitter
|
|
// on slow macOS/linux runners under -race.
|
|
cache := newDocCache(4, 50*time.Millisecond)
|
|
cache.Set("A", "alpha")
|
|
time.Sleep(100 * time.Millisecond)
|
|
if _, ok := cache.Get("A"); ok {
|
|
t.Errorf("A should be expired")
|
|
}
|
|
}
|
|
|
|
func TestDocCache_UpdateExistingKey(t *testing.T) {
|
|
cache := newDocCache(4, time.Minute)
|
|
cache.Set("A", "alpha")
|
|
cache.Set("A", "alpha-v2") // same key, new value
|
|
|
|
if got, ok := cache.Get("A"); !ok || got != "alpha-v2" {
|
|
t.Errorf("update: got (%q, %v), want (\"alpha-v2\", true)", got, ok)
|
|
}
|
|
}
|