package main import ( "path/filepath" "testing" "git.yeij.top/AskaEth/Cyrene_v2/internal/config" "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/memory" modelpkg "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model" ) // testRuntimeLLM builds a provider stack from env alone (the pointed-at // models.json does not exist), so a test controls the active group through the // flat LLM config exactly as a keyless .env boot would. func testRuntimeLLM(t *testing.T, env config.LLMConfig) *modelpkg.RuntimeLLM { t.Helper() return modelpkg.NewRuntimeLLM(filepath.Join(t.TempDir(), "models.json"), env, testLog()) } // TestBuildMemoryExtractor_LLMWhenCredentials proves a configured provider group // selects the LLM extractor (design/03 §1). The extractor is role-agnostic // (design/22 §4.2): it is built without a persona and the loop stamps the turn's // role onto each extracted item, so multi-role extraction cannot be filed under // the default role. func TestBuildMemoryExtractor_LLMWhenCredentials(t *testing.T) { t.Parallel() a := &app{ log: testLog(), llm: testRuntimeLLM(t, config.LLMConfig{ BaseURL: "https://api.example.com/v1", APIKey: "sk-test", Model: "gpt-4o-mini", }), } if _, ok := a.buildMemoryExtractor().(*memory.LLMExtractor); !ok { t.Fatalf("extractor = %T, want *memory.LLMExtractor (credentials configured)", a.buildMemoryExtractor()) } } // TestBuildMemoryExtractor_ManualWhenNoCredentials proves the keyless fallback: a // boot without an API key (and without a local endpoint) keeps the deterministic // ManualExtractor instead of firing a doomed model call on every turn. func TestBuildMemoryExtractor_ManualWhenNoCredentials(t *testing.T) { t.Parallel() a := &app{log: testLog(), llm: testRuntimeLLM(t, config.LLMConfig{})} if _, ok := a.buildMemoryExtractor().(*memory.ManualExtractor); !ok { t.Fatalf("extractor = %T, want *memory.ManualExtractor (no usable llm group)", a.buildMemoryExtractor()) } bare := &app{log: testLog()} if _, ok := bare.buildMemoryExtractor().(*memory.ManualExtractor); !ok { t.Fatalf("extractor = %T, want *memory.ManualExtractor (nil provider stack)", bare.buildMemoryExtractor()) } } // TestLLMGroupUsable_KeylessLocalEndpoint locks the ollama-style case: a local // OpenAI-compatible server needs no API key, so extraction stays LLM-backed. func TestLLMGroupUsable_KeylessLocalEndpoint(t *testing.T) { t.Parallel() cases := []struct { name string env config.LLMConfig ready bool }{ {name: "api key", env: config.LLMConfig{BaseURL: "https://api.example.com/v1", APIKey: "sk", Model: "m"}, ready: true}, {name: "loopback keyless", env: config.LLMConfig{BaseURL: "http://127.0.0.1:11434/v1", Model: "llama3"}, ready: true}, {name: "localhost keyless", env: config.LLMConfig{BaseURL: "http://localhost:1234/v1", Model: "m"}, ready: true}, {name: "private keyless", env: config.LLMConfig{BaseURL: "http://192.168.1.20:11434/v1", Model: "m"}, ready: true}, {name: "remote keyless", env: config.LLMConfig{BaseURL: "https://api.example.com/v1", Model: "m"}, ready: false}, {name: "no model", env: config.LLMConfig{BaseURL: "https://api.example.com/v1", APIKey: "sk"}, ready: false}, {name: "no base url", env: config.LLMConfig{APIKey: "sk", Model: "m"}, ready: false}, } for _, tc := range cases { tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() if got := llmGroupUsable(testRuntimeLLM(t, tc.env)); got != tc.ready { t.Fatalf("llmGroupUsable = %v, want %v", got, tc.ready) } }) } }