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
66 lines
2.2 KiB
Go
66 lines
2.2 KiB
Go
package chatpipeline
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
)
|
|
|
|
type expandChunkRepo struct {
|
|
interfaces.ChunkRepository
|
|
chunks map[string]*types.Chunk
|
|
children map[string][]*types.Chunk
|
|
}
|
|
|
|
func (r *expandChunkRepo) ListChunksByParentIDs(
|
|
_ context.Context, _ uint64, parentIDs []string,
|
|
) ([]*types.Chunk, error) {
|
|
var chunks []*types.Chunk
|
|
for _, parentID := range parentIDs {
|
|
chunks = append(chunks, r.children[parentID]...)
|
|
}
|
|
return chunks, nil
|
|
}
|
|
|
|
func (r *expandChunkRepo) ListChunksByID(
|
|
_ context.Context, _ uint64, ids []string,
|
|
) ([]*types.Chunk, error) {
|
|
chunks := make([]*types.Chunk, 0, len(ids))
|
|
for _, id := range ids {
|
|
if chunk := r.chunks[id]; chunk != nil {
|
|
chunks = append(chunks, chunk)
|
|
}
|
|
}
|
|
return chunks, nil
|
|
}
|
|
|
|
func TestExpandShortContextKeepsSourceCoordinates(t *testing.T) {
|
|
repo := &expandChunkRepo{chunks: map[string]*types.Chunk{
|
|
"prev": {ID: "prev", KnowledgeID: "doc", ChunkType: types.ChunkTypeText, Content: "edited previous body", NextChunkID: "base"},
|
|
"base": {ID: "base", KnowledgeID: "doc", ChunkType: types.ChunkTypeText, Content: "edited base body", PreChunkID: "prev", NextChunkID: "next", StartAt: 100, EndAt: 120},
|
|
"next": {ID: "next", KnowledgeID: "doc", ChunkType: types.ChunkTypeText, Content: "edited next body", PreChunkID: "base"},
|
|
}}
|
|
plugin := &PluginMerge{chunkRepo: repo}
|
|
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1))
|
|
result := &types.SearchResult{
|
|
ID: "base", KnowledgeID: "doc", ChunkType: string(types.ChunkTypeText),
|
|
Content: "edited base body", StartAt: 100, EndAt: 120,
|
|
}
|
|
|
|
got := plugin.expandShortContextWithNeighbors(ctx, &types.ChatManage{}, []*types.SearchResult{result})
|
|
if len(got) != 1 {
|
|
t.Fatalf("result count = %d, want 1", len(got))
|
|
}
|
|
if got[0].StartAt != 100 || got[0].EndAt != 120 {
|
|
t.Fatalf("source coordinates changed to [%d,%d)", got[0].StartAt, got[0].EndAt)
|
|
}
|
|
for _, want := range []string{"edited previous body", "edited base body", "edited next body"} {
|
|
if !strings.Contains(got[0].Content, want) {
|
|
t.Fatalf("expanded content missing %q: %q", want, got[0].Content)
|
|
}
|
|
}
|
|
}
|