-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmoke_test.zig
More file actions
307 lines (261 loc) · 14.7 KB
/
Copy pathsmoke_test.zig
File metadata and controls
307 lines (261 loc) · 14.7 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
//
// Game Server Admin — End-to-end smoke test
//
// Exercises the full FFI pipeline without requiring a live game server
// or running VeriSimDB instance. This test verifies that all modules
// compose correctly:
//
// init → profile load → probe (localhost) → config parse →
// A2ML emit → octad JSON → VeriSimDB client construction → shutdown
//
// Run with: zig build test-smoke
const std = @import("std");
const testing = std.testing;
// Import all modules via the gsa build module (see build.zig addImport)
const gsa = @import("gsa");
const main = gsa;
const probe = gsa.probe;
const config_extract = gsa.config_extract;
const a2ml_emit = gsa.a2ml_emit;
const verisimdb_client = gsa.verisimdb_client;
const server_actions = gsa.server_actions;
const game_profiles = gsa.game_profiles;
const groove_client = gsa.groove_client;
// ═══════════════════════════════════════════════════════════════════════════════
// 1. Full pipeline: init → profile → probe → extract → emit → octad → shutdown
// ═══════════════════════════════════════════════════════════════════════════════
test "smoke: full pipeline without live services" {
const allocator = testing.allocator;
// --- Step 1: Create a GsaHandle (simulates gossamer_gsa_init) ---
const handle = try main.GsaHandle.create(allocator, "http://[::1]:8090", "");
defer handle.destroy();
try testing.expect(handle.initialized);
try testing.expectEqualStrings("http://[::1]:8090", handle.verisimdb_url);
// --- Step 2: Parse a game profile from inline A2ML ---
const mc_profile_a2ml =
\\@game-profile(id="minecraft-java", name="Minecraft Java Edition", engine="Java"):
\\ @ports:
\\ @port(name="query", number=25565, protocol="TCP"):@end
\\ @port(name="rcon", number=25575, protocol="TCP"):@end
\\ @end
\\ @protocol(type="minecraft-query", variant="MC"):@end
\\ @config(format="key-value", path="/data/server.properties"):
\\ @field(key="motd", type="string", label="Message of the Day"):
\\ @default("A Minecraft Server") @end
\\ @end
\\ @field(key="max-players", type="int", label="Max Players"):
\\ @constraint(min=1, max=1000) @end
\\ @default(20) @end
\\ @end
\\ @field(key="difficulty", type="enum", label="Difficulty"):
\\ @options("peaceful", "easy", "normal", "hard") @end
\\ @default("easy") @end
\\ @end
\\ @end
\\@end
;
var profile = try game_profiles.parseA2MLProfile(mc_profile_a2ml, allocator);
defer profile.deinit();
try testing.expectEqualStrings("minecraft-java", profile.id);
try testing.expectEqual(@as(usize, 2), profile.ports.items.len);
try testing.expectEqual(@as(usize, 3), profile.field_defs.items.len);
// --- Step 3: Simulate a probe result (no live server needed) ---
var probe_result = probe.ProbeResult{};
probe_result.setGameId("minecraft-java");
probe_result.setVersion("1.21.4");
probe_result.port = 25565;
probe_result.protocol = .MinecraftQuery;
probe_result.latency_ms = 12;
// --- Step 4: Parse config from raw text ---
const raw_config =
\\# Minecraft server properties
\\server-name=Smoke Test Server
\\max-players=32
\\difficulty=normal
\\rcon.password=secret123
\\enable-command-block=true
\\view-distance=16
;
// Detect format
const format = config_extract.detectFormat(raw_config);
try testing.expectEqual(config_extract.ConfigFormat.KeyValue, format);
// Build a ParsedConfig manually (simulating extractConfig)
var config = config_extract.ParsedConfig.init(allocator, .KeyValue, "/data/server.properties");
defer config.deinit();
try config.addField("server-name", "Smoke Test Server", "string", "Server Name", "A Minecraft Server", null, null, false);
try config.addField("max-players", "32", "int", "Max Players", "20", 1.0, 1000.0, false);
try config.addField("difficulty", "normal", "enum", "Difficulty", "easy", null, null, false);
try config.addField("rcon.password", "secret123", "secret", "RCON Password", "", null, null, true);
try config.addField("enable-command-block", "true", "bool", "Command Blocks", "false", null, null, false);
try config.addField("view-distance", "16", "int", "View Distance", "10", 2.0, 32.0, false);
try testing.expectEqual(@as(usize, 6), config.fields.items.len);
// --- Step 5: Emit A2ML ---
const a2ml = try a2ml_emit.emitA2ML(allocator, "mc-smoke-1", "minecraft-java", &config, &profile);
defer allocator.free(a2ml);
// Verify A2ML structure
try testing.expect(std.mem.indexOf(u8, a2ml, "SPDX-License-Identifier") != null);
try testing.expect(std.mem.indexOf(u8, a2ml, "@server(") != null);
try testing.expect(std.mem.indexOf(u8, a2ml, "mc-smoke-1") != null);
try testing.expect(std.mem.indexOf(u8, a2ml, "Smoke Test Server") != null);
try testing.expect(std.mem.indexOf(u8, a2ml, "[REDACTED]") != null); // secret must be redacted
try testing.expect(std.mem.indexOf(u8, a2ml, "secret123") == null); // actual secret must NOT appear
// --- Step 6: Parse A2ML back and verify round-trip ---
var restored = try a2ml_emit.parseA2ML(allocator, a2ml);
defer restored.deinit();
try testing.expectEqual(@as(usize, 6), restored.fields.items.len);
try testing.expectEqualStrings("server-name", restored.fields.items[0].key);
try testing.expectEqualStrings("Smoke Test Server", restored.fields.items[0].value);
// --- Step 7: Build octad JSON for VeriSimDB ---
const spatial = [3]f64{ 51.5074, -0.1278, 0.0 }; // London
const octad_json = try verisimdb_client.serverToOctadJson(
allocator,
"mc-smoke-1",
"minecraft-java",
&config,
&probe_result,
spatial,
);
defer allocator.free(octad_json);
// Verify all 8 facets are present in the octad
try testing.expect(std.mem.indexOf(u8, octad_json, "\"metadata\"") != null);
try testing.expect(std.mem.indexOf(u8, octad_json, "\"config\"") != null);
try testing.expect(std.mem.indexOf(u8, octad_json, "\"probe\"") != null);
try testing.expect(std.mem.indexOf(u8, octad_json, "\"spatial\"") != null);
try testing.expect(std.mem.indexOf(u8, octad_json, "\"temporal\"") != null);
try testing.expect(std.mem.indexOf(u8, octad_json, "\"relational\"") != null);
try testing.expect(std.mem.indexOf(u8, octad_json, "\"provenance\"") != null);
try testing.expect(std.mem.indexOf(u8, octad_json, "\"semantic\"") != null);
// Verify secrets are redacted in the octad too
try testing.expect(std.mem.indexOf(u8, octad_json, "[REDACTED]") != null);
try testing.expect(std.mem.indexOf(u8, octad_json, "secret123") == null);
// --- Step 8: Construct VeriSimDB client (no actual connection) ---
var vdb_client = verisimdb_client.VeriSimClient.init(allocator, "http://[::1]:8090");
defer vdb_client.deinit();
try testing.expectEqualStrings("http://[::1]:8090", vdb_client.base_url);
// --- Step 9: Track the server in the handle ---
try handle.trackServer("mc-smoke-1", .{
.host = "127.0.0.1",
.port = 25565,
.protocol = .MinecraftQuery,
.last_seen_ms = std.time.milliTimestamp(),
.healthy = true,
});
const conn = handle.getConnection("mc-smoke-1") orelse return error.NotFound;
try testing.expectEqual(@as(u16, 25565), conn.port);
try testing.expect(conn.healthy);
// --- Step 10: Config diff (simulate config change) ---
var new_config = config_extract.ParsedConfig.init(allocator, .KeyValue, "/data/server.properties");
defer new_config.deinit();
try new_config.addField("server-name", "Updated Server", "string", "Server Name", "A Minecraft Server", null, null, false);
try new_config.addField("max-players", "64", "int", "Max Players", "20", 1.0, 1000.0, false);
try new_config.addField("difficulty", "normal", "enum", "Difficulty", "easy", null, null, false);
try new_config.addField("rcon.password", "secret123", "secret", "RCON Password", "", null, null, true);
try new_config.addField("enable-command-block", "true", "bool", "Command Blocks", "false", null, null, false);
try new_config.addField("view-distance", "16", "int", "View Distance", "10", 2.0, 32.0, false);
const diffs = try a2ml_emit.diffConfigs(allocator, &config, &new_config);
defer a2ml_emit.freeDiffs(allocator, diffs);
try testing.expectEqual(@as(usize, 2), diffs.len); // server-name and max-players changed
}
// ═══════════════════════════════════════════════════════════════════════════════
// 2. Multi-format config detection sweep
// ═══════════════════════════════════════════════════════════════════════════════
test "smoke: all 8 config formats are detectable" {
const formats = [_]struct { input: []const u8, expected: config_extract.ConfigFormat }{
.{ .input = "<?xml version=\"1.0\"?><Config/>", .expected = .XML },
.{ .input = "[Server]\nname = Test", .expected = .INI },
.{ .input = "{\"name\": \"Test\"}", .expected = .JSON },
.{ .input = "export NAME=\"Test\"", .expected = .ENV },
.{ .input = "[[plugins]]\nname = \"test\"", .expected = .TOML },
.{ .input = "local config = {\n name = \"Test\",\n}", .expected = .Lua },
.{ .input = "name=Test\nport=25565", .expected = .KeyValue },
// YAML detection falls through to KeyValue in current impl
// (acceptable — YAML support is deferred)
};
for (formats) |f| {
const detected = config_extract.detectFormat(f.input);
try testing.expectEqual(f.expected, detected);
}
}
// ═══════════════════════════════════════════════════════════════════════════════
// 3. Profile registry lifecycle
// ═══════════════════════════════════════════════════════════════════════════════
test "smoke: profile registry add and query" {
const allocator = testing.allocator;
var registry = game_profiles.ProfileRegistry.init(allocator);
defer registry.deinit();
// Starts empty
const empty_list = try registry.listProfiles(allocator);
defer allocator.free(empty_list);
try testing.expectEqualStrings("[]", empty_list);
// Add a profile
const profile_a2ml =
\\@game-profile(id="valheim", name="Valheim", engine="Unity"):
\\ @ports:
\\ @port(name="game", number=2456, protocol="UDP"):@end
\\ @end
\\ @protocol(type="steam-query", variant="SteamWorks"):@end
\\ @config(format="env", path="/config/valheim/server.env"):
\\ @field(key="SERVER_NAME", type="string", label="Server Name"):
\\ @default("My Valheim Server") @end
\\ @end
\\ @end
\\@end
;
try registry.registerFromText(profile_a2ml);
// Verify it's in the registry
const list = try registry.listProfiles(allocator);
defer allocator.free(list);
try testing.expect(std.mem.indexOf(u8, list, "valheim") != null);
// Query by ID
const profile = registry.getProfile("valheim");
try testing.expect(profile != null);
try testing.expectEqualStrings("Valheim", profile.?.name);
}
// ═══════════════════════════════════════════════════════════════════════════════
// 4. Error handling: uninitialised state
// ═══════════════════════════════════════════════════════════════════════════════
test "smoke: error buffer survives full pipeline" {
// Set and retrieve errors across module boundaries
main.setErrorStr("probe timeout on 10.0.0.1:25565");
const err1 = std.mem.span(main.gossamer_gsa_last_error());
try testing.expectEqualStrings("probe timeout on 10.0.0.1:25565", err1);
main.setError("config parse failed: {s} at line {d}", .{ "unexpected token", @as(u32, 42) });
const err2 = std.mem.span(main.gossamer_gsa_last_error());
try testing.expectEqualStrings("config parse failed: unexpected token at line 42", err2);
main.clearError();
const err3 = std.mem.span(main.gossamer_gsa_last_error());
try testing.expectEqual(@as(usize, 0), err3.len);
}
// ═══════════════════════════════════════════════════════════════════════════════
// 5. Known ports coverage for supported game profiles
// ═══════════════════════════════════════════════════════════════════════════════
test "smoke: all 18 game profiles have matching known ports" {
// Ports from the 18 game profiles in profiles/
const profile_ports = [_]u16{
25565, // minecraft-java
19132, // minecraft-bedrock
2456, // valheim
28015, // rust
7777, // ark
27015, // cs2, gmod, tf2
2302, // dayz
34197, // factorio
7777, // terraria (shared with ARK)
27015, // barotrauma (shared)
16261, // project-zomboid
21025, // starbound
10999, // dst
};
for (profile_ports) |port| {
var found = false;
for (probe.KNOWN_PORTS) |kp| {
if (kp.port == port) {
found = true;
break;
}
}
try testing.expect(found);
}
}