|
1 | 1 | package llamacpp |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 | ) |
6 | 7 |
|
7 | 8 | func TestExtractLlamaCppError(t *testing.T) { |
8 | 9 | tests := []struct { |
9 | | - name string |
10 | | - input string |
11 | | - expected string |
| 10 | + name string |
| 11 | + input string |
| 12 | + expected string |
| 13 | + expectedPrefix string |
| 14 | + expectTruncated bool |
12 | 15 | }{ |
13 | 16 | { |
14 | | - name: "Metal buffer allocation failure", |
15 | | - input: "ggml_metal_buffer_init: error: failed to allocate buffer, size = 2048.00 MiB", |
16 | | - expected: "not enough GPU memory to load the model (Metal)", |
| 17 | + name: "Metal buffer allocation failure", |
| 18 | + input: "ggml_metal_buffer_init: error: failed to allocate buffer, size = 2048.00 MiB", |
| 19 | + expected: "not enough GPU memory to load the model (Metal)\n\nVerbose output:\n" + |
| 20 | + "ggml_metal_buffer_init: error: failed to allocate buffer, size = 2048.00 MiB", |
17 | 21 | }, |
18 | 22 | { |
19 | | - name: "cudaMalloc OOM", |
20 | | - input: "ggml_backend_cuda_buffer_type_alloc_buffer: allocating 12.50 MiB on device 1: cudaMalloc failed: out of memory", |
21 | | - expected: "not enough GPU memory to load the model (CUDA)", |
| 23 | + name: "cudaMalloc OOM", |
| 24 | + input: "ggml_backend_cuda_buffer_type_alloc_buffer: allocating 12.50 MiB on device 1: cudaMalloc failed: out of memory", |
| 25 | + expected: "not enough GPU memory to load the model (CUDA)\n\nVerbose output:\n" + |
| 26 | + "ggml_backend_cuda_buffer_type_alloc_buffer: allocating 12.50 MiB on device 1: cudaMalloc failed: out of memory", |
22 | 27 | }, |
23 | 28 | { |
24 | 29 | name: "loading error", |
25 | 30 | input: `common_init_from_params: failed to load model '/models/model.gguf' |
26 | 31 | main: exiting due to model loading error`, |
27 | | - expected: "failed to load model", |
| 32 | + expected: "failed to load model\n\nVerbose output:\n" + |
| 33 | + "common_init_from_params: failed to load model '/models/model.gguf'\n" + |
| 34 | + "main: exiting due to model loading error", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "input with leading/trailing whitespace", |
| 38 | + input: "\n\n ggml_metal_buffer_init: error: failed to allocate buffer, size = 2048.00 MiB \n\n", |
| 39 | + expected: "not enough GPU memory to load the model (Metal)\n\nVerbose output:\n" + |
| 40 | + "ggml_metal_buffer_init: error: failed to allocate buffer, size = 2048.00 MiB", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "truncation of large output", |
| 44 | + input: "ggml_metal_buffer_init: error: failed to allocate buffer, size = 2048.00 MiB\n" + strings.Repeat("verbose log line\n", 500), |
| 45 | + expectedPrefix: "not enough GPU memory to load the model (Metal)\n\nVerbose output:\n" + |
| 46 | + "ggml_metal_buffer_init: error: failed to allocate buffer, size = 2048.00 MiB\n", |
| 47 | + expectTruncated: true, |
28 | 48 | }, |
29 | 49 | } |
30 | 50 |
|
31 | 51 | for _, tt := range tests { |
32 | 52 | t.Run(tt.name, func(t *testing.T) { |
33 | 53 | result := ExtractLlamaCppError(tt.input) |
34 | | - if result != tt.expected { |
| 54 | + if tt.expectTruncated { |
| 55 | + if !strings.HasPrefix(result, tt.expectedPrefix) { |
| 56 | + t.Errorf("ExtractLlamaCppError() = %q, want prefix %q", result, tt.expectedPrefix) |
| 57 | + } |
| 58 | + if !strings.HasSuffix(result, "...[truncated]") { |
| 59 | + t.Errorf("ExtractLlamaCppError() = %q, want suffix ...[truncated]", result) |
| 60 | + } |
| 61 | + } else if result != tt.expected { |
35 | 62 | t.Errorf("ExtractLlamaCppError() = %q, want %q", result, tt.expected) |
36 | 63 | } |
37 | 64 | }) |
|
0 commit comments