|
1 | 1 | local h = require("tests.helpers") |
2 | 2 | local adapter |
3 | 3 |
|
| 4 | +local child = MiniTest.new_child_neovim() |
4 | 5 | local new_set = MiniTest.new_set |
5 | 6 | T = new_set() |
6 | 7 |
|
@@ -360,4 +361,164 @@ T["Ollama adapter"]["OLLAMA_HOST"]["fallback to localhost when OLLAMA_HOST is no |
360 | 361 | h.eq("http://localhost:11434", adapter.env_replaced.url) |
361 | 362 | end |
362 | 363 |
|
| 364 | +T["Ollama get_models"] = new_set({ |
| 365 | + hooks = { |
| 366 | + pre_case = function() |
| 367 | + h.child_start(child) |
| 368 | + child.lua([[ |
| 369 | + h = require('tests.helpers') |
| 370 | + config = require('tests.config') |
| 371 | + require('codecompanion').setup(config) |
| 372 | + ]]) |
| 373 | + end, |
| 374 | + post_once = child.stop, |
| 375 | + }, |
| 376 | +}) |
| 377 | + |
| 378 | +T["Ollama get_models"]["choices() extracts context_window into meta"] = function() |
| 379 | + local result = child.lua([[ |
| 380 | + local get_models = require("codecompanion.adapters.http.ollama.get_models") |
| 381 | + local Curl = require("plenary.curl") |
| 382 | +
|
| 383 | + local tags_body = vim.json.encode({ |
| 384 | + models = { { name = "llama3.1:latest" } }, |
| 385 | + }) |
| 386 | + local show_body = vim.json.encode({ |
| 387 | + capabilities = { "completion" }, |
| 388 | + details = { |
| 389 | + family = "llama", |
| 390 | + families = { "llama" }, |
| 391 | + format = "gguf", |
| 392 | + parameter_size = "8.0B", |
| 393 | + parent_model = "", |
| 394 | + quantization_level = "Q4_0", |
| 395 | + }, |
| 396 | + model_info = { |
| 397 | + ["llama.context_length"] = 8192, |
| 398 | + }, |
| 399 | + }) |
| 400 | +
|
| 401 | + Curl.get = function(url, opts) |
| 402 | + if opts.callback then |
| 403 | + opts.callback({ status = 200, body = tags_body }) |
| 404 | + end |
| 405 | + end |
| 406 | + Curl.post = function(url, opts) |
| 407 | + if opts.callback then |
| 408 | + opts.callback({ status = 200, body = show_body }) |
| 409 | + end |
| 410 | + end |
| 411 | +
|
| 412 | + local adapter = require("codecompanion.adapters.http").extend("ollama", { |
| 413 | + schema = { |
| 414 | + model = { |
| 415 | + default = "llama3.1:latest", |
| 416 | + choices = { "llama3.1:latest" }, |
| 417 | + }, |
| 418 | + }, |
| 419 | + }) |
| 420 | +
|
| 421 | + return get_models.choices(adapter, { async = false }) |
| 422 | + ]]) |
| 423 | + |
| 424 | + h.eq({ context_window = 8192 }, result["llama3.1:latest"].meta) |
| 425 | + h.eq(false, result["llama3.1:latest"].opts.can_reason) |
| 426 | + h.eq(false, result["llama3.1:latest"].opts.can_use_tools) |
| 427 | + h.eq(false, result["llama3.1:latest"].opts.has_vision) |
| 428 | +end |
| 429 | + |
| 430 | +T["Ollama get_models"]["choices() extracts capabilities into opts"] = function() |
| 431 | + local result = child.lua([[ |
| 432 | + local get_models = require("codecompanion.adapters.http.ollama.get_models") |
| 433 | + local Curl = require("plenary.curl") |
| 434 | +
|
| 435 | + local tags_body = vim.json.encode({ |
| 436 | + models = { { name = "qwq:latest" } }, |
| 437 | + }) |
| 438 | + local show_body = vim.json.encode({ |
| 439 | + capabilities = { "completion", "thinking", "tools", "vision" }, |
| 440 | + details = { |
| 441 | + family = "qwen3", |
| 442 | + families = { "qwen3" }, |
| 443 | + format = "gguf", |
| 444 | + parameter_size = "32.8B", |
| 445 | + parent_model = "", |
| 446 | + quantization_level = "Q4_0", |
| 447 | + }, |
| 448 | + model_info = { |
| 449 | + ["qwen3.context_length"] = 40960, |
| 450 | + }, |
| 451 | + }) |
| 452 | +
|
| 453 | + Curl.get = function(url, opts) |
| 454 | + if opts.callback then |
| 455 | + opts.callback({ status = 200, body = tags_body }) |
| 456 | + end |
| 457 | + end |
| 458 | + Curl.post = function(url, opts) |
| 459 | + if opts.callback then |
| 460 | + opts.callback({ status = 200, body = show_body }) |
| 461 | + end |
| 462 | + end |
| 463 | +
|
| 464 | + local adapter = require("codecompanion.adapters.http").extend("ollama", { |
| 465 | + schema = { |
| 466 | + model = { |
| 467 | + default = "qwq:latest", |
| 468 | + choices = { "qwq:latest" }, |
| 469 | + }, |
| 470 | + }, |
| 471 | + }) |
| 472 | +
|
| 473 | + return get_models.choices(adapter, { async = false }) |
| 474 | + ]]) |
| 475 | + |
| 476 | + h.eq({ context_window = 40960 }, result["qwq:latest"].meta) |
| 477 | + h.eq(true, result["qwq:latest"].opts.can_reason) |
| 478 | + h.eq(true, result["qwq:latest"].opts.can_use_tools) |
| 479 | + h.eq(true, result["qwq:latest"].opts.has_vision) |
| 480 | +end |
| 481 | + |
| 482 | +T["Ollama get_models"]["choices() handles missing model_info gracefully"] = function() |
| 483 | + local result = child.lua([[ |
| 484 | + local get_models = require("codecompanion.adapters.http.ollama.get_models") |
| 485 | + local Curl = require("plenary.curl") |
| 486 | +
|
| 487 | + local tags_body = vim.json.encode({ |
| 488 | + models = { { name = "custom:latest" } }, |
| 489 | + }) |
| 490 | + local show_body = vim.json.encode({ |
| 491 | + capabilities = { "completion" }, |
| 492 | + details = { |
| 493 | + family = "custom", |
| 494 | + }, |
| 495 | + }) |
| 496 | +
|
| 497 | + Curl.get = function(url, opts) |
| 498 | + if opts.callback then |
| 499 | + opts.callback({ status = 200, body = tags_body }) |
| 500 | + end |
| 501 | + end |
| 502 | + Curl.post = function(url, opts) |
| 503 | + if opts.callback then |
| 504 | + opts.callback({ status = 200, body = show_body }) |
| 505 | + end |
| 506 | + end |
| 507 | +
|
| 508 | + local adapter = require("codecompanion.adapters.http").extend("ollama", { |
| 509 | + schema = { |
| 510 | + model = { |
| 511 | + default = "custom:latest", |
| 512 | + choices = { "custom:latest" }, |
| 513 | + }, |
| 514 | + }, |
| 515 | + }) |
| 516 | +
|
| 517 | + return get_models.choices(adapter, { async = false }) |
| 518 | + ]]) |
| 519 | + |
| 520 | + h.eq(nil, result["custom:latest"].meta) |
| 521 | + h.eq("custom:latest", result["custom:latest"].formatted_name) |
| 522 | +end |
| 523 | + |
363 | 524 | return T |
0 commit comments