generated from wippyai/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_handler_test.lua
More file actions
247 lines (207 loc) · 8.65 KB
/
config_handler_test.lua
File metadata and controls
247 lines (207 loc) · 8.65 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
local test = require("test")
local json = require("json")
local registry = require("registry")
local NS = "wippy.facade:"
local REQ_NAMES: {string} = {
"fe_facade_url", "fe_entry_path", "session_type",
"history_mode", "show_admin", "start_nav_open", "allow_select_model",
"hide_nav_bar", "disable_right_panel",
"custom_css", "app_title", "app_icon", "app_name", "login_path",
}
local function setup_registry(overrides: {[string]: string}?)
local defaults: {[string]: string} = {
fe_facade_url = "https://front.wippy.ai",
fe_entry_path = "/iframe.html",
session_type = "non-persistent",
history_mode = "hash",
show_admin = "true",
start_nav_open = "false",
allow_select_model = "false",
hide_nav_bar = "false",
disable_right_panel = "false",
custom_css = "",
app_title = "Wippy",
app_icon = "wippy:logo",
app_name = "Wippy AI",
login_path = "/login.html",
}
if overrides then
for k, v in pairs(overrides) do
defaults[k] = v
end
end
local snap = registry.snapshot()
local changes = snap:changes()
for name, value in pairs(defaults) do
changes:create({
id = NS .. name,
kind = "ns.requirement",
data = { default = value },
})
end
changes:apply()
end
local function teardown_registry()
local snap = registry.snapshot()
local changes = snap:changes()
for _, name in ipairs(REQ_NAMES) do
changes:delete(NS .. name)
end
changes:apply()
end
local function derive_ws_url(api_url: string): string
if api_url == "" then
return ""
end
return api_url:gsub("^https://", "wss://"):gsub("^http://", "ws://")
end
local function define_tests()
test.describe("config handler", function()
test.describe("ws url derivation", function()
test.it("derives ws from http api url", function()
test.eq(derive_ws_url("http://localhost:8085"), "ws://localhost:8085")
end)
test.it("derives wss from https api url", function()
test.eq(derive_ws_url("https://app.wippy.ai"), "wss://app.wippy.ai")
end)
test.it("returns empty for empty url", function()
test.eq(derive_ws_url(""), "")
end)
test.it("preserves path in ws url", function()
test.eq(derive_ws_url("http://localhost:8085/api"), "ws://localhost:8085/api")
end)
end)
test.describe("iframe URL construction", function()
test.it("builds iframe URL from facade_url and entry_path", function()
local facade_url = "https://front.wippy.ai"
local entry_path = "/iframe.html"
local iframe_url = facade_url .. entry_path .. "?waitForCustomConfig"
test.eq(iframe_url, "https://front.wippy.ai/iframe.html?waitForCustomConfig")
end)
test.it("extracts iframe origin from facade URL", function()
local facade_url = "https://web-host.wippy.ai/webcomponents-1.0.12"
local origin = facade_url:match("^(https?://[^/]+)")
test.eq(origin, "https://web-host.wippy.ai")
end)
test.it("returns empty iframe URL when facade_url is empty", function()
local facade_url = ""
local iframe_url = ""
if facade_url ~= "" then
iframe_url = facade_url .. "/iframe.html?waitForCustomConfig"
end
test.eq(iframe_url, "")
end)
end)
test.describe("feature flags", function()
test.it("show_admin defaults to true", function()
test.is_true("true" ~= "false")
end)
test.it("show_admin can be disabled", function()
test.is_false("false" ~= "false")
end)
test.it("allow_select_model defaults to false", function()
test.is_false("false" == "true")
end)
test.it("allow_select_model can be enabled", function()
test.is_true("true" == "true")
end)
test.it("start_nav_open defaults to false", function()
test.is_false("false" == "true")
end)
test.it("hide_nav_bar defaults to false", function()
test.is_false("false" == "true")
end)
test.it("disable_right_panel defaults to false", function()
test.is_false("false" == "true")
end)
end)
test.describe("registry integration", function()
test.before_each(function()
setup_registry()
end)
test.after_each(function()
teardown_registry()
end)
test.it("reads requirement values from registry", function()
local entry = registry.get(NS .. "app_title")
test.not_nil(entry)
test.eq(entry.data.default, "Wippy")
end)
test.it("reads overridden values", function()
local snap = registry.snapshot()
local changes = snap:changes()
changes:update({
id = NS .. "app_title",
kind = "ns.requirement",
data = { default = "Custom App" },
})
changes:apply()
local entry = registry.get(NS .. "app_title")
test.eq(entry.data.default, "Custom App")
end)
test.it("returns all default requirement values", function()
local entry = registry.get(NS .. "fe_facade_url")
test.eq(entry.data.default, "https://front.wippy.ai")
entry = registry.get(NS .. "fe_entry_path")
test.eq(entry.data.default, "/iframe.html")
entry = registry.get(NS .. "login_path")
test.eq(entry.data.default, "/login.html")
entry = registry.get(NS .. "session_type")
test.eq(entry.data.default, "non-persistent")
entry = registry.get(NS .. "history_mode")
test.eq(entry.data.default, "hash")
end)
end)
test.describe("config JSON structure", function()
test.it("builds complete config object", function()
local config = {
facade_url = "https://front.wippy.ai",
iframe_origin = "https://front.wippy.ai",
iframe_url = "https://front.wippy.ai/iframe.html?waitForCustomConfig",
api_url = "http://localhost:8085",
ws_url = "ws://localhost:8085",
feature = {
session_type = "non-persistent",
history_mode = "hash",
show_admin = true,
allow_select_model = false,
start_nav_open = false,
hide_nav_bar = false,
disable_right_panel = false,
},
customization = {
custom_css = "@import url('https://fonts.example.com');",
css_variables = { ["p-primary"] = "#3b82f6" },
i18n = {
app = {
title = "Wippy",
icon = "wippy:logo",
appName = "Wippy AI",
},
},
icons = { logo = { body = "<path/>", width = 24, height = 24 } },
},
login_path = "/login.html",
}
local body, err = json.encode(config)
test.is_nil(err)
test.not_nil(body)
local decoded, derr = json.decode(body)
test.is_nil(derr)
test.eq(decoded.facade_url, "https://front.wippy.ai")
test.eq(decoded.api_url, "http://localhost:8085")
test.eq(decoded.ws_url, "ws://localhost:8085")
test.eq(decoded.feature.session_type, "non-persistent")
test.is_true(decoded.feature.show_admin)
test.is_false(decoded.feature.allow_select_model)
test.eq(decoded.customization.i18n.app.title, "Wippy")
test.eq(decoded.login_path, "/login.html")
end)
end)
end)
end
local run_cases = test.run_cases(define_tests)
local function run(options)
return run_cases(options)
end
return { run = run }