summaryrefslogtreecommitdiff
path: root/_selfcheck.lua
blob: 33d05701762990b985ec7197558defdbd311a0bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
-- Offline: panto lua _selfcheck.lua (or Lua 5.4).
local here = debug.getinfo(1, "S").source:match("^@(.*)/[^/]+$") or "."
local entry = assert(loadfile(here .. "/init.lua"))()
assert(entry.name == "copilot" and type(entry.configure) == "function")
assert(entry.activate == nil, "configure-only extension")

local function contains(actual, expected)
    for key, value in pairs(expected) do
        if type(value) == "table" then
            assert(type(actual[key]) == "table", key .. " must be a table")
            contains(actual[key], value)
        else
            assert(actual[key] == value, key .. " default mismatch")
        end
    end
end

local empty = {}
entry.configure(empty)
assert(next(empty) == nil, "no implicit auth/providers")

local untouched = { providers = {
    absent = {}, disabled = { panto_copilot_extension = false },
    string_marker = { panto_copilot_extension = "true" },
}, auth = { absent = {}, disabled = { panto_copilot_extension = false } } }
entry.configure(untouched)
assert(next(untouched.providers.absent) == nil)
assert(untouched.providers.disabled.style == nil and untouched.providers.string_marker.style == nil)
assert(next(untouched.auth.absent) == nil and untouched.auth.disabled.type == nil)

local config = {
    auth = { first = { panto_copilot_extension = true }, second = { panto_copilot_extension = true } },
    providers = {
        first = { panto_copilot_extension = true, auth = "second" },
        second = { panto_copilot_extension = true, auth = "first" },
    },
}
entry.configure(config)
contains(config.auth.first, {
    type = "oauth_device",
    client_id = "Iv1.b507a08c87ecfe98",
    device_code_url = "https://github.com/login/device/code",
    token_url = "https://github.com/login/oauth/access_token",
    scope = "read:user",
    exchange_url = "https://api.github.com/copilot_internal/v2/token",
    exchange_expires_path = "expires_at",
    exchange_base_url_path = "endpoints.api",
})
contains(config.providers.first, {
    style = "openai_responses",
    model_catalog_name = "github-copilot",
    extra_headers = {
        ["User-Agent"] = "GitHubCopilotChat/0.35.0",
        ["Editor-Version"] = "vscode/1.107.0",
        ["Editor-Plugin-Version"] = "copilot-chat/0.35.0",
        ["Copilot-Integration-Id"] = "vscode-chat",
    },
    endpoints = {
    openai_chat = { base_url = "https://api.individual.githubcopilot.com" },
    openai_responses = { base_url = "https://api.individual.githubcopilot.com" },
    anthropic_messages = { base_url = "https://api.individual.githubcopilot.com" },
    },
})
assert(config.providers.first.auth == "second" and config.providers.second.auth == "first")
assert(config.providers.first.extra_headers ~= config.providers.second.extra_headers)
config.providers.first.extra_headers.custom = "first-only"
assert(config.providers.second.extra_headers.custom == nil, "instance defaults must not alias")
entry.configure(config)
assert(config.providers.first.extra_headers.custom == "first-only", "repeatable configuration")

local fresh = { providers = { third = { panto_copilot_extension = true } } }
entry.configure(fresh)
assert(fresh.providers.third.auth == nil, "never invent auth linkage")
assert(fresh.providers.third.extra_headers.custom == nil, "no cross-load mutable defaults")

local override = {
    auth = { login = { panto_copilot_extension = true, client_id = "custom-client", key = "${env:CUSTOM_KEY}" } },
    providers = { custom = {
        panto_copilot_extension = true,
        auth = "login",
        model_catalog_name = "custom-catalog",
        extra_headers = { ["user-agent"] = "custom-header", custom = "${session:id}" },
        endpoints = { openai_responses = {
            base_url = "https://gateway.example/v1",
            extra_headers = { local_header = "kept" },
        } },
    } },
}
entry.configure(override)
local custom = override.providers.custom
assert(override.auth.login.client_id == "custom-client")
assert(override.auth.login.key == "${env:CUSTOM_KEY}", "raw key reference preserved")
assert(custom.auth == "login" and custom.model_catalog_name == "custom-catalog")
assert(custom.base_url == nil and custom.dialect == nil, "endpoint form stays valid")
assert(custom.endpoints.openai_responses.base_url == "https://gateway.example/v1")
assert(custom.endpoints.openai_responses.extra_headers.local_header == "kept")
assert(custom.extra_headers["user-agent"] == "custom-header")
assert(custom.extra_headers["User-Agent"] == nil, "case-insensitive header override")
assert(custom.extra_headers.custom == "${session:id}", "raw session reference preserved")

local flat = { providers = { custom = {
    panto_copilot_extension = true, auth = "login", style = "openai_chat",
    base_url = "https://gateway.example/v1",
} } }
entry.configure(flat)
assert(flat.providers.custom.endpoints == nil, "explicit flat provider stays flat")
assert(flat.providers.custom.style == "openai_chat")
assert(flat.providers.custom.base_url == "https://gateway.example/v1")

local invalid = { providers = { marked = { panto_copilot_extension = true, extra_headers = false } } }
entry.configure(invalid)
assert(invalid.providers.marked.extra_headers == false, "explicit false remains untouched")

print("panto-copilot: ok")