|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package mcpserver |
| 5 | + |
| 6 | +import "testing" |
| 7 | + |
| 8 | +func TestFileURIToPath(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + input string |
| 12 | + want string |
| 13 | + }{ |
| 14 | + // ── Unix paths ────────────────────────────────────────────────────── |
| 15 | + { |
| 16 | + name: "unix three-slash URI", |
| 17 | + input: "file:///home/user/workspace", |
| 18 | + want: "/home/user/workspace", |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "unix three-slash URI with subdirectory", |
| 22 | + input: "file:///var/log/ghqr", |
| 23 | + want: "/var/log/ghqr", |
| 24 | + }, |
| 25 | + |
| 26 | + // ── Windows paths (uppercase drive letter) ────────────────────────── |
| 27 | + { |
| 28 | + name: "windows three-slash URI uppercase drive", |
| 29 | + input: "file:///C:/Users/gh/workspace", |
| 30 | + want: "C:/Users/gh/workspace", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "windows three-slash URI uppercase Z drive", |
| 34 | + input: "file:///Z:/Projects", |
| 35 | + want: "Z:/Projects", |
| 36 | + }, |
| 37 | + |
| 38 | + // ── Windows paths (lowercase drive letter) ────────────────────────── |
| 39 | + { |
| 40 | + name: "windows three-slash URI lowercase drive", |
| 41 | + input: "file:///c:/Users/gh/workspace", |
| 42 | + want: "c:/Users/gh/workspace", |
| 43 | + }, |
| 44 | + |
| 45 | + // ── Windows paths with percent-encoded colon (%3A) ────────────────── |
| 46 | + // VS Code sends file:///c%3A/Users/... per Issue 1. |
| 47 | + { |
| 48 | + name: "windows percent-encoded colon lowercase", |
| 49 | + input: "file:///c%3A/Users/gh/workspace", |
| 50 | + want: "c:/Users/gh/workspace", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "windows percent-encoded colon uppercase", |
| 54 | + input: "file:///C%3A/Users/gh/workspace", |
| 55 | + want: "C:/Users/gh/workspace", |
| 56 | + }, |
| 57 | + |
| 58 | + // ── Paths that are not file URIs ────────────────────────────────── |
| 59 | + { |
| 60 | + name: "plain path passthrough", |
| 61 | + input: "C:/Users/gh", |
| 62 | + want: "C:/Users/gh", |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "unix plain path passthrough", |
| 66 | + input: "/home/user", |
| 67 | + want: "/home/user", |
| 68 | + }, |
| 69 | + |
| 70 | + // ── Edge cases ─────────────────────────────────────────────────────── |
| 71 | + { |
| 72 | + name: "empty string", |
| 73 | + input: "", |
| 74 | + want: "", |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "file URI with spaces (percent-encoded)", |
| 78 | + input: "file:///home/user/my%20project", |
| 79 | + want: "/home/user/my project", |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + got := fileURIToPath(tt.input) |
| 86 | + if got != tt.want { |
| 87 | + t.Errorf("fileURIToPath(%q) = %q, want %q", tt.input, got, tt.want) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestIsASCIILetter(t *testing.T) { |
| 94 | + for b := byte('a'); b <= 'z'; b++ { |
| 95 | + if !isASCIILetter(b) { |
| 96 | + t.Errorf("isASCIILetter(%q) = false, want true", b) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + for b := byte('A'); b <= 'Z'; b++ { |
| 101 | + if !isASCIILetter(b) { |
| 102 | + t.Errorf("isASCIILetter(%q) = false, want true", b) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + nonLetters := []byte{'0', '9', ':', '/', '\\', ' ', '-', '_'} |
| 107 | + for _, b := range nonLetters { |
| 108 | + if isASCIILetter(b) { |
| 109 | + t.Errorf("isASCIILetter(%q) = true, want false", b) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments