Files
ragflow/internal/ingestion/task/golden_compare_test.go
Jack b59d6e8ba1 refactor(ingestion/task): extract index-doc mapping into task/indexdoc package (#17749)
## Summary
Extract the pipeline-output → search-engine index document mapping
helpers out of the `task` package into a dedicated, dependency-light
leaf package `internal/ingestion/task/indexdoc`.

These functions are pure transforms (they only depend on
`common`/`utility`) and are not task-orchestration concerns:
- `NormalizeChunks`, `DeepCopyChunks` (was unexported `deepCopyChunks`),
`toChunkMaps` → `indexdoc/normalize.go`
- `ProcessChunksForPipeline`, `RenameTextToContentWithWeight`,
`GetEmbeddingTokenConsumption`, `cleanupConsumedChunkFields`,
`mergeChunkMetadata`, `processChunkPositions`,
`AggregateTableDocMetadata`, `resolveTableColumnConfig` →
`indexdoc/process.go`
- `AddPositions` → `indexdoc/position.go`
- `EmbeddingTokenConsumptionKey` constant → `indexdoc/constants.go`
(task/constants.go keeps only `GRAPH_RAPTOR_FAKE_DOC_ID`)

Call sites in `pipeline_executor.go` and `golden_compare.go` now
reference the `indexdoc` package; package-task tests qualify the moved
symbols.

## Why
The `task` package had grown into a "orchestration + pure mapping +
debug" mix. Splitting the pure mapping helpers into a leaf package
sharpens package boundaries, removes a misleading top-level
`ingestion/chunk` candidate (there are already `parser/chunk` and
`service/chunk`), and lets the golden tool / future reuse pull in the
mapping logic without dragging in `task`'s `dao`/`engine`/`service`
dependency graph (Go subpackage import does not pull in the parent).

## Test plan
- `build.sh --test ./internal/ingestion/task/...` — **green** (task
4.7s, indexdoc 0.007s), matching the pre-change baseline.
- `gofmt` clean; `build.sh` builds both `ragflow-cli` and
`ragflow_server` successfully.
- Integration/E2E tiers are delegated to CI (need real MySQL/MinIO/ES
services).

Note: `pipeline_e2e_test.go` has a **pre-existing** compile error
(`server.ElasticsearchConfig` / `server.InfinityConfig` are now defined
under `internal/server/config/`, not re-exported by `internal/server`).
This is unrelated to this change — the diff to that file is only the
added `indexdoc` import and the qualified `EmbeddingTokenConsumptionKey`
reference.
2026-08-04 10:05:27 +08:00

80 lines
2.2 KiB
Go

package task
import (
"testing"
"time"
indexdoc "ragflow/internal/ingestion/task/indexdoc"
)
func TestProcessPipelineOutputForGolden_Markdown(t *testing.T) {
input := map[string]any{
"markdown": "# Title\n\nContent",
}
result, err := ProcessPipelineOutputForGolden(input, "doc-1", "kb-1", "sample.md")
if err != nil {
t.Fatalf("ProcessPipelineOutputForGolden: %v", err)
}
if len(result.NormalizedChunks) != 1 {
t.Fatalf("normalized len = %d, want 1", len(result.NormalizedChunks))
}
if got := result.NormalizedChunks[0]["text"]; got != "# Title\n\nContent" {
t.Fatalf("normalized text = %v, want markdown string", got)
}
if len(result.ProcessedChunks) != 1 {
t.Fatalf("processed len = %d, want 1", len(result.ProcessedChunks))
}
if got := result.ProcessedChunks[0]["content_with_weight"]; got != "# Title\n\nContent" {
t.Fatalf("content_with_weight = %v, want markdown string", got)
}
if _, exists := result.ProcessedChunks[0]["text"]; exists {
t.Fatal("processed chunk should not keep text key")
}
}
func TestProcessChunksForPipeline_StableFields(t *testing.T) {
now := time.Date(2026, 7, 3, 20, 0, 0, 0, time.FixedZone("CST", 8*3600))
chunks := []map[string]any{
{
"text": "hello",
"questions": "Q1\nQ2",
"keywords": "kw1,kw2",
"summary": "sum",
"metadata": map[string]any{"author": "Alice"},
},
}
meta, err := indexdoc.ProcessChunksForPipeline(chunks, "doc-1", "kb-1", "sample.md", now)
if err != nil {
t.Fatalf("ProcessChunksForPipeline: %v", err)
}
chunk := chunks[0]
if chunk["doc_id"] != "doc-1" {
t.Fatalf("doc_id = %v", chunk["doc_id"])
}
if chunk["docnm_kwd"] != "sample.md" {
t.Fatalf("docnm_kwd = %v", chunk["docnm_kwd"])
}
if chunk["content_with_weight"] != "hello" {
t.Fatalf("content_with_weight = %v", chunk["content_with_weight"])
}
if _, ok := chunk["id"].(string); !ok {
t.Fatalf("id should be string, got %T", chunk["id"])
}
if _, exists := chunk["questions"]; exists {
t.Fatal("questions should be removed")
}
if _, exists := chunk["keywords"]; exists {
t.Fatal("keywords should be removed")
}
if _, exists := chunk["summary"]; exists {
t.Fatal("summary should be removed")
}
if meta["author"] != "Alice" {
t.Fatalf("metadata merge failed: %v", meta)
}
}