|
| 1 | +package fabriqbrain |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/xraph/fabriq/core/query" |
| 8 | + "github.com/xraph/fabriq/core/registry" |
| 9 | + log "github.com/xraph/go-utils/log" |
| 10 | +) |
| 11 | + |
| 12 | +// stubFabric satisfies fabricFacade. Its embedded query.Fabric is nil: none of |
| 13 | +// its methods are exercised because buildToolkit's dims check returns before the |
| 14 | +// fabric is touched. Registry is non-nil so NewToolkit's nil-registry guard passes. |
| 15 | +type stubFabric struct { |
| 16 | + query.Fabric |
| 17 | + reg *registry.Registry |
| 18 | +} |
| 19 | + |
| 20 | +func (s stubFabric) Registry() *registry.Registry { return s.reg } |
| 21 | + |
| 22 | +// dimsEmbedder reports a fixed dimensionality; Embed is never called in these tests. |
| 23 | +type dimsEmbedder struct{ dims int } |
| 24 | + |
| 25 | +func (e dimsEmbedder) Dims() int { return e.dims } |
| 26 | +func (dimsEmbedder) Embed(context.Context, []string) ([][]float32, error) { |
| 27 | + return nil, nil |
| 28 | +} |
| 29 | + |
| 30 | +// captureLogger records Error-level messages so a test can assert a failure was |
| 31 | +// surfaced rather than silently dropped. The embedded no-op Logger supplies the |
| 32 | +// rest of the interface. |
| 33 | +type captureLogger struct { |
| 34 | + log.Logger |
| 35 | + errors []string |
| 36 | +} |
| 37 | + |
| 38 | +func (c *captureLogger) Error(msg string, _ ...log.Field) { c.errors = append(c.errors, msg) } |
| 39 | + |
| 40 | +func newStubFabric() stubFabric { return stubFabric{reg: registry.New()} } |
| 41 | + |
| 42 | +// A configured vector-dims value is threaded into the toolkit: a 1536-dim |
| 43 | +// embedder is accepted only because WithVectorDims(1536) overrides the 768 default. |
| 44 | +func TestBuildToolkit_ThreadsVectorDims(t *testing.T) { |
| 45 | + c := applyOptions([]Option{ |
| 46 | + WithEmbedder(dimsEmbedder{dims: 1536}), |
| 47 | + WithVectorDims(1536), |
| 48 | + }) |
| 49 | + if _, err := buildToolkit(newStubFabric(), c); err != nil { |
| 50 | + t.Fatalf("buildToolkit with matching WithVectorDims = %v, want nil", err) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Without WithVectorDims, fabriq's 768 default applies, so a 1536-dim embedder |
| 55 | +// is a mismatch and buildToolkit must report it. |
| 56 | +func TestBuildToolkit_RejectsDimMismatchAgainstDefault(t *testing.T) { |
| 57 | + c := applyOptions([]Option{WithEmbedder(dimsEmbedder{dims: 1536})}) |
| 58 | + if _, err := buildToolkit(newStubFabric(), c); err == nil { |
| 59 | + t.Fatal("buildToolkit with 1536-dim embedder vs 768 default = nil, want dims-mismatch error") |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// A dims mismatch in the wiring path must be logged (surfaced), not silently |
| 64 | +// swallowed into a no-op brain. |
| 65 | +func TestResolveToolkit_LogsDimsMismatch(t *testing.T) { |
| 66 | + cl := &captureLogger{Logger: log.NewNoopLogger()} |
| 67 | + c := applyOptions([]Option{ |
| 68 | + WithEmbedder(dimsEmbedder{dims: 1536}), |
| 69 | + WithLogger(cl), |
| 70 | + }) |
| 71 | + tk := resolveToolkit(newStubFabric(), c) |
| 72 | + if tk != nil { |
| 73 | + t.Fatalf("resolveToolkit on dims mismatch = %v, want nil", tk) |
| 74 | + } |
| 75 | + if len(cl.errors) == 0 { |
| 76 | + t.Fatal("dims mismatch was silently dropped; expected an error log") |
| 77 | + } |
| 78 | +} |
0 commit comments