Files
Enterprise-Knowledge-Base/internal/llmresource/registry_test.go
T
wizardchen 6d14cc3536 feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.

Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 17:02:43 +08:00

44 lines
1.4 KiB
Go

package llmresource
import (
"testing"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/stretchr/testify/require"
)
func TestRegistryRoundTripAndDeduplicate(t *testing.T) {
r := NewRegistry()
ref := "resource://AbCdEfGhIjKlMnOpQrStUv"
encoded := r.EncodeText("![a](" + ref + ") and " + ref)
require.Equal(t, "![a](res:0001) and res:0001", encoded)
require.Equal(t, "![a]("+ref+") and "+ref, r.DecodeText(encoded))
}
func TestRegistryAliasesLegacyPhysicalReferencesDuringRollout(t *testing.T) {
r := NewRegistry()
ref := "storage://c0d93536-702c-4977-aa5e-fe670073c3cb/local://10000/exports/image.png"
encoded := r.EncodeText("![image](" + ref + ")")
require.Equal(t, "![image](res:0001)", encoded)
require.Equal(t, "![image]("+ref+")", r.DecodeText(encoded))
}
func TestRegistryEncodesMessageCopies(t *testing.T) {
r := NewRegistry()
ref := "resource://AbCdEfGhIjKlMnOpQrStUv"
original := []chat.Message{{Role: "tool", Content: ref}}
encoded := r.EncodeMessages(original)
require.Equal(t, ref, original[0].Content)
require.Equal(t, "res:0001", encoded[0].Content)
}
func TestStreamDecoderRestoresSplitAlias(t *testing.T) {
r := NewRegistry()
ref := "resource://AbCdEfGhIjKlMnOpQrStUv"
require.Equal(t, "res:0001", r.EncodeText(ref))
d := NewStreamDecoder(r)
require.Equal(t, "before ", d.Feed("before res:0"))
require.Equal(t, ref+" afte", d.Feed("001 after"))
require.Equal(t, "r", d.Flush())
}