Files
xinmi/internal/application/service/knowledge_content_binding_test.go
yawan f7fc992f57
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

153 lines
5.2 KiB
Go

package service
import (
"context"
"errors"
"strings"
"testing"
"time"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
// resolvingCatalog answers Resolve from a fixed handle → tenant map and records
// every Bind, which is all bindContentResources exercises.
type resolvingCatalog struct {
tenantByRef map[string]uint64
binds []bindCall
}
func (c *resolvingCatalog) Resolve(_ context.Context, ref string) (*types.StoredResource, error) {
tenantID, ok := c.tenantByRef[ref]
if !ok {
return nil, errors.New("resource not found")
}
handle, _ := types.ParseResourcePath(ref)
return &types.StoredResource{ID: handle, Handle: handle, TenantID: tenantID}, nil
}
func (c *resolvingCatalog) Bind(_ context.Context, ref, ownerType, ownerID, relation string) error {
c.binds = append(c.binds, bindCall{ref, ownerType, ownerID, relation})
return nil
}
func (c *resolvingCatalog) Register(
context.Context, uint64, string, interfaces.ResourceRegistration,
) (string, error) {
return "", errors.New("unused")
}
func (c *resolvingCatalog) ResolvePath(
_ context.Context, value string,
) (string, *types.StoredResource, error) {
return value, nil, nil
}
func (c *resolvingCatalog) Release(context.Context, string, string, string) (int64, error) {
return -1, nil
}
func (c *resolvingCatalog) MarkDeleted(context.Context, string) error { return nil }
func (c *resolvingCatalog) CreateAccessGrant(
context.Context, string, time.Duration,
) (string, error) {
return "", errors.New("unused")
}
func (c *resolvingCatalog) ResolveAccessGrant(
context.Context, string,
) (*types.StoredResource, error) {
return nil, errors.New("unused")
}
func contentRef(char string) string {
return types.BuildResourcePath(strings.Repeat(char, types.ResourceHandleLength))
}
func TestManualRepublishKeepsReferencedImageOwnership(t *testing.T) {
kept, removed := contentRef("a"), contentRef("b")
knowledge := &types.Knowledge{Type: types.KnowledgeTypeManual}
if err := knowledge.SetManualMetadata(types.NewManualKnowledgeMetadata("![image]("+kept+")", "published", 2)); err != nil {
t.Fatal(err)
}
obsolete := obsoleteManualImages(knowledge, []string{kept, removed})
if len(obsolete) != 1 || obsolete[0] != removed {
t.Fatalf("referenced body image would be deleted: %v", obsolete)
}
}
type scopedBindingCatalog struct {
resolvingCatalog
}
func (c *scopedBindingCatalog) AuthorizeFileRead(context.Context, string, interfaces.ResourceFileScope) error {
return errors.New("file belongs to another user's private session")
}
func TestBindContentResourcesRejectsSameTenantPrivateReference(t *testing.T) {
ref := contentRef("z")
catalog := &scopedBindingCatalog{resolvingCatalog{tenantByRef: map[string]uint64{ref: 7}}}
svc := &knowledgeService{resourceCatalog: catalog}
svc.bindContentResources(context.Background(), 7, "my-document", "![stolen]("+ref+")")
if len(catalog.binds) != 0 {
t.Fatal("an inaccessible same-tenant resource must not gain a new owner")
}
}
// Saving a chat answer into the knowledge base must claim the files it shows
// without copying them: the assistant message keeps its own claim, so either
// side can be deleted without breaking the other.
func TestBindContentResourcesClaimsEveryReferencedFile(t *testing.T) {
chart := contentRef("a")
table := contentRef("b")
catalog := &resolvingCatalog{tenantByRef: map[string]uint64{chart: 7, table: 7}}
svc := &knowledgeService{resourceCatalog: catalog}
content := "## 结论\n\n![评分](" + chart + ")\n\n数据见 [表格](" + table + ")"
svc.bindContentResources(context.Background(), 7, "kn-1", content)
if len(catalog.binds) != 2 {
t.Fatalf("binds = %v, want one per reference", catalog.binds)
}
for i, want := range []string{chart, table} {
got := catalog.binds[i]
if got.ref != want {
t.Fatalf("binds[%d].ref = %q, want %q", i, got.ref, want)
}
if got.ownerType != types.ResourceOwnerKnowledge || got.ownerID != "kn-1" {
t.Fatalf("binds[%d] owner = (%q, %q), want (%q, kn-1)",
i, got.ownerType, got.ownerID, types.ResourceOwnerKnowledge)
}
if got.relation != types.ResourceRelationAttachment {
t.Fatalf("binds[%d].relation = %q, want %q",
i, got.relation, types.ResourceRelationAttachment)
}
}
}
// A handle pasted from another workspace must not be claimed: binding it would
// hand the caller a file it may not read.
func TestBindContentResourcesSkipsForeignAndUnknownHandles(t *testing.T) {
mine := contentRef("c")
theirs := contentRef("d")
unknown := contentRef("e")
catalog := &resolvingCatalog{tenantByRef: map[string]uint64{mine: 7, theirs: 9}}
svc := &knowledgeService{resourceCatalog: catalog}
content := "![a](" + mine + ") ![b](" + theirs + ") ![c](" + unknown + ")"
svc.bindContentResources(context.Background(), 7, "kn-1", content)
if len(catalog.binds) != 1 || catalog.binds[0].ref != mine {
t.Fatalf("binds = %v, want only %q", catalog.binds, mine)
}
}
func TestBindContentResourcesIsInertWithoutCatalog(t *testing.T) {
svc := &knowledgeService{}
// Must not panic; a deployment without a resource registry has nothing to
// claim and keeps the pre-binding behaviour.
svc.bindContentResources(context.Background(), 7, "kn-1", "![a]("+contentRef("f")+")")
}