Files
xinmi/internal/application/service/knowledge_process_parent_child_test.go
T
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

201 lines
5.4 KiB
Go

package service
import (
"context"
"testing"
"github.com/Tencent/WeKnora/internal/models/embedding"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/hibiken/asynq"
"github.com/stretchr/testify/require"
)
type parentChildKnowledgeRepo struct {
interfaces.KnowledgeRepository
knowledge *types.Knowledge
}
func (r *parentChildKnowledgeRepo) GetKnowledgeByID(
context.Context, uint64, string,
) (*types.Knowledge, error) {
return r.knowledge, nil
}
func (r *parentChildKnowledgeRepo) UpdateKnowledge(
context.Context, *types.Knowledge,
) error {
return nil
}
type parentChildChunkService struct {
interfaces.ChunkService
created []*types.Chunk
}
func (s *parentChildChunkService) DeleteChunksByKnowledgeID(context.Context, string) error {
return nil
}
func (s *parentChildChunkService) CreateChunks(_ context.Context, chunks []*types.Chunk) error {
s.created = append([]*types.Chunk(nil), chunks...)
return nil
}
type parentChildModelService struct {
interfaces.ModelService
embedder embedding.Embedder
}
func (s parentChildModelService) GetEmbeddingModel(context.Context, string) (embedding.Embedder, error) {
return s.embedder, nil
}
type parentChildEmbedder struct{}
func (parentChildEmbedder) Embed(context.Context, string) ([]float32, error) {
return []float32{1}, nil
}
func (parentChildEmbedder) BatchEmbed(context.Context, []string) ([][]float32, error) {
return [][]float32{{1}}, nil
}
func (parentChildEmbedder) BatchEmbedWithPool(
context.Context, embedding.Embedder, []string,
) ([][]float32, error) {
return [][]float32{{1}}, nil
}
func (parentChildEmbedder) GetModelName() string { return "parent-child-test" }
func (parentChildEmbedder) GetDimensions() int { return 1 }
func (parentChildEmbedder) GetModelID() string { return "parent-child-test" }
type parentChildRetrieveEngine struct {
interfaces.RetrieveEngineService
indexed []*types.IndexInfo
}
func (e *parentChildRetrieveEngine) EngineType() types.RetrieverEngineType {
return types.PostgresRetrieverEngineType
}
func (e *parentChildRetrieveEngine) Support() []types.RetrieverType {
return []types.RetrieverType{types.VectorRetrieverType}
}
func (e *parentChildRetrieveEngine) DeleteByKnowledgeIDList(
context.Context, []string, int, string,
) error {
return nil
}
func (e *parentChildRetrieveEngine) EstimateStorageSize(
context.Context, embedding.Embedder, []*types.IndexInfo, []types.RetrieverType,
) int64 {
return 0
}
func (e *parentChildRetrieveEngine) BatchIndex(
_ context.Context,
_ embedding.Embedder,
infos []*types.IndexInfo,
_ []types.RetrieverType,
) error {
e.indexed = append([]*types.IndexInfo(nil), infos...)
return nil
}
type parentChildRetrieveRegistry struct {
interfaces.RetrieveEngineRegistry
engine interfaces.RetrieveEngineService
}
func (r parentChildRetrieveRegistry) GetRetrieveEngineService(
types.RetrieverEngineType,
) (interfaces.RetrieveEngineService, error) {
return r.engine, nil
}
type parentChildGraphRepo struct {
interfaces.RetrieveGraphRepository
}
func (parentChildGraphRepo) DelGraph(context.Context, []types.NameSpace) error {
return nil
}
type parentChildTenantRepo struct {
interfaces.TenantRepository
}
func (parentChildTenantRepo) AdjustStorageUsed(context.Context, uint64, int64) error {
return nil
}
type parentChildTaskEnqueuer struct{}
func (parentChildTaskEnqueuer) Enqueue(*asynq.Task, ...asynq.Option) (*asynq.TaskInfo, error) {
return nil, nil
}
func TestProcessChunksIndexesEveryTextChild(t *testing.T) {
knowledge := &types.Knowledge{
ID: "knowledge-1",
TenantID: 1,
KnowledgeBaseID: "kb-1",
ParseStatus: types.ParseStatusProcessing,
}
chunkService := &parentChildChunkService{}
retrieveEngine := &parentChildRetrieveEngine{}
tenant := &types.Tenant{
ID: 1,
RetrieverEngines: types.RetrieverEngines{Engines: []types.RetrieverEngineParams{
{
RetrieverType: types.VectorRetrieverType,
RetrieverEngineType: types.PostgresRetrieverEngineType,
},
}},
}
ctx := context.WithValue(context.Background(), types.TenantInfoContextKey, tenant)
svc := &knowledgeService{
repo: &parentChildKnowledgeRepo{knowledge: knowledge},
chunkService: chunkService,
modelService: parentChildModelService{embedder: parentChildEmbedder{}},
retrieveEngine: parentChildRetrieveRegistry{engine: retrieveEngine},
graphEngine: parentChildGraphRepo{},
tenantRepo: parentChildTenantRepo{},
task: parentChildTaskEnqueuer{},
}
kb := &types.KnowledgeBase{
ID: "kb-1",
TenantID: 1,
EmbeddingModelID: "embedding-1",
IndexingStrategy: types.IndexingStrategy{VectorEnabled: true},
}
chunks := []types.ParsedChunk{
{Content: "linked child", Seq: 0, Start: 0, End: 12, ParentIndex: 0},
{Content: "standalone child", Seq: 1, Start: 12, End: 28, ParentIndex: -1},
}
svc.processChunks(ctx, kb, knowledge, chunks, ProcessChunksOptions{
ParentChunks: []types.ParsedParentChunk{
{Content: "parent context", Seq: 0, Start: 0, End: 28},
},
})
var textChunkIDs []string
for _, chunk := range chunkService.created {
if chunk.ChunkType == types.ChunkTypeText {
textChunkIDs = append(textChunkIDs, chunk.ID)
}
}
require.Len(t, textChunkIDs, 2)
indexedSourceIDs := make([]string, 0, len(retrieveEngine.indexed))
for _, info := range retrieveEngine.indexed {
indexedSourceIDs = append(indexedSourceIDs, info.SourceID)
}
require.ElementsMatch(t, textChunkIDs, indexedSourceIDs)
}