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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// ── bru.* API for Tropel ───────────────────────────────────────────────────
// Bruno-compat peer view over the shared runtime state.
// Bruno's API shape: `bru.getEnvVar()`, `req.setHeader()`, `res.getBody()`
// (three objects rather than one namespace — P4b). Frozen compat: Bruno.
// Uses the same __tropel_trp_* bridges as the pm binding; the state model
// is binding-agnostic (P4b core).
//
// Frozen compat: this layer reproduces Bruno's documented scripting API
// and does NOT gain features from trp.* or pm.*. See TROPEL_MODULARIZATION_TODO.md
// §P4b: "compatibility layers must be frozen, not co-evolved."
(function () {
var g = typeof globalThis !== 'undefined' ? globalThis : null;
if (!g) return;
// ── bru namespace ──────────────────────────────────────────────────────
var bru = {};
// Environment
bru.getEnvVar = function (key) {
if (typeof __tropel_trp_environment_get === 'function') {
// W2 line 182: the bridge returns the value JSON-encoded ("..."
// with literal quotes) so the correct JS type round-trips — the
// old raw return meant getEnvVar('baseUrl') came back WITH the
// quotes and every URL built from it was malformed. Parse like
// pm.environment.get (pm.js:26).
var raw = __tropel_trp_environment_get(key);
if (raw === null || raw === undefined) return null;
try { return JSON.parse(raw); } catch (e) { return raw; }
}
return null;
};
// The three setters below MUST JSON-encode, because their getters
// JSON.parse. `String(value)` made set/get non-inverse: setVar('id','1234')
// read back as the NUMBER 1234, and setVar('u',{id:7}) read back as the
// string "[object Object]" — a collection doing
// `bru.setVar('p', res.getBody()); req.setBody(bru.getVar('p'))` put that
// literal string on the wire and still ran green. pm.js has always
// stringified here; this is the sibling that was missed.
function encodeBruValue(value) {
if (value === undefined) return '';
try {
return JSON.stringify(value);
} catch (e) {
return String(value);
}
}
bru.setEnvVar = function (key, value) {
if (typeof __tropel_trp_environment_set === 'function') {
__tropel_trp_environment_set(key, encodeBruValue(value));
}
};
// Runtime variables — Bruno's bru.getVar/setVar are RUNTIME-scope
// (in-memory, per collection run), NOT collection scope. TROPEL_PARITY_BRUNO.md
// §2: they used to map to the collection_vars bridges, so a runtime var set
// by one request could not be read by the next (the core request-chaining
// idiom silently broke). They now route through the pm.variables store
// (__tropel_trp_variables_*, the same fall-through lookup pm.variables uses).
bru.getVar = function (key) {
if (typeof __tropel_trp_variables_get === 'function') {
var raw = __tropel_trp_variables_get(key);
if (raw === null || raw === undefined) return undefined;
try { return JSON.parse(raw); } catch (e) { return raw; }
}
return undefined;
};
bru.setVar = function (key, value) {
if (typeof __tropel_trp_variables_set === 'function') {
__tropel_trp_variables_set(key, encodeBruValue(value));
}
};
bru.hasVar = function (key) {
// Single bridge round-trip: getVar returns undefined on a miss.
var v = bru.getVar(key);
return v !== undefined;
};
bru.deleteVar = function (key) {
if (typeof __tropel_trp_variables_unset === 'function') {
__tropel_trp_variables_unset(key);
}
};
// NOTE (deleteAllVars cascade): deleteVar → variables_unset removes from
// local + collection + environment + globals, so on a key collision this
// also clears an env/global var of the same name. A scoped runtime-only
// unset needs a bridge change (TROPEL_PARITY_BRUNO.md §7).
bru.deleteAllVars = function () {
var all = bru.getAllVars();
for (var k in all) {
if (Object.prototype.hasOwnProperty.call(all, k)) bru.deleteVar(k);
}
};
// getAllVars: reads the LOCAL runtime store that setVar writes
// (__tropel_trp_variables_to_object). W2 line 182: it used to read
// collection_vars while setVar wrote local_vars — the aliasing comment
// claimed the stores alias at the Rust level; they don't, so a runtime
// var set via setVar never appeared in getAllVars.
bru.getAllVars = function () {
if (typeof __tropel_trp_variables_to_object === 'function') {
var map = __tropel_trp_variables_to_object() || {};
var out = {};
for (var k in map) {
if (Object.prototype.hasOwnProperty.call(map, k)) {
var raw = map[k];
try { out[k] = JSON.parse(raw); } catch (e) { out[k] = raw; }
}
}
return out;
}
return {};
};
// Explicit collection-scope accessors (TROPEL_PARITY_BRUNO.md §2). Bruno
// distinguishes bru.getVar/setVar (RUNTIME scope) from the collection
// scope — getCollectionVar/setCollectionVar/hasCollectionVar/delete* map
// to the __tropel_trp_collection_vars_* bridges, independent of the
// runtime store used by getVar/setVar.
bru.getCollectionVar = function (key) {
if (typeof __tropel_trp_collection_vars_get === 'function') {
var raw = __tropel_trp_collection_vars_get(key);
if (raw === null || raw === undefined) return undefined;
try { return JSON.parse(raw); } catch (e) { return raw; }
}
return undefined;
};
bru.setCollectionVar = function (key, value) {
if (typeof __tropel_trp_collection_vars_set === 'function') {
__tropel_trp_collection_vars_set(key, encodeBruValue(value));
}
};
bru.hasCollectionVar = function (key) {
if (typeof __tropel_trp_collection_vars_has === 'function') {
return __tropel_trp_collection_vars_has(key);
}
return false;
};
bru.deleteCollectionVar = function (key) {
if (typeof __tropel_trp_collection_vars_unset === 'function') {
__tropel_trp_collection_vars_unset(key);
}
};
bru.deleteAllCollectionVars = function () {
if (typeof __tropel_trp_collection_vars_to_object !== 'function' ||
typeof __tropel_trp_collection_vars_unset !== 'function') {
return;
}
var map = __tropel_trp_collection_vars_to_object() || {};
for (var k in map) {
if (Object.prototype.hasOwnProperty.call(map, k)) {
__tropel_trp_collection_vars_unset(k);
}
}
};
// Request body
bru.getReqBody = function () {
if (typeof __tropel_trp_request_body === 'function') {
return __tropel_trp_request_body();
}
return null;
};
bru.setReqBody = function (body) {
if (typeof __tropel_trp_request_body_set === 'function') {
__tropel_trp_request_body_set(body === undefined ? '' : String(body));
}
};
// Request headers
bru.getReqHeader = function (name) {
if (typeof __tropel_trp_request_header_get === 'function') {
return __tropel_trp_request_header_get(name);
}
return null;
};
bru.setReqHeader = function (name, value) {
if (typeof __tropel_trp_request_header_set === 'function') {
__tropel_trp_request_header_set(name, String(value));
}
};
// Response
bru.getResBody = function () {
if (typeof __tropel_trp_response_body === 'function') {
return __tropel_trp_response_body();
}
return null;
};
bru.getResHeader = function (name) {
if (typeof __tropel_trp_response_header === 'function') {
return __tropel_trp_response_header(name);
}
return null;
};
bru.getResStatus = function () {
if (typeof __tropel_trp_response_code === 'function') {
return __tropel_trp_response_code();
}
return 0;
};
bru.getResTime = function () {
if (typeof __tropel_trp_response_time === 'function') {
return __tropel_trp_response_time();
}
return 0;
};
// Environment name (stub — tropel doesn't track the env name independently)
bru.getEnvName = function () {
return null;
};
// Assertion — Bruno's bru.assert(expression, errorMessage) evaluates the
// expression string as code, matching user expectations.
bru.assert = function (expr, errorMessage) {
var passed = false;
if (typeof expr === 'function') {
passed = expr();
} else if (typeof expr === 'string') {
try { passed = eval(expr); } catch (e) { passed = false; }
} else {
passed = !!expr;
}
if (typeof __tropel_trp_test === 'function') {
// W2 line 182: the bridge takes (name, passed: BOOL, tags) — an
// int 1/0 has NO bool coercion in rquickjs (pm.js:506-508 warns
// about exactly this rule), so `passed ? 1 : 0` threw on EVERY
// call. Pass a real bool + the empty tags string like pm.test.
__tropel_trp_test(
'bru.assert: ' + (errorMessage || String(expr)),
passed ? true : false,
''
);
}
};
// Sleep (delegates to the native sleep bridge, in ms).
// NOTE: Bruno's bru.sleep(ms) is async (returns a Promise); the
// synchronous implementation is a QuickJS embedding limitation.
bru.sleep = function (ms) {
if (typeof __tropel_native_sleep === 'function' && typeof ms === 'number' && ms > 0) {
__tropel_native_sleep(ms);
}
};
// Logging
bru.log = function () {
if (typeof console !== 'undefined' && typeof console.log === 'function') {
console.log.apply(console, arguments);
}
};
// Flow control — Bruno's bru.next() maps to setNextRequest
bru.next = function (requestName) {
if (typeof __tropel_trp_set_next_request === 'function') {
__tropel_trp_set_next_request(requestName);
}
};
// ── bru.getTestResults / bru.getAssertionResults (TR-478) ───────────────
//
// These existed only in the API client's own prelude, so a script calling
// either worked in the app and died on a bare ReferenceError under a load
// run and on the agent tier — the same one-script-two-answers split
// `fetch` had before TR-475.
//
// They are NOT the same kind of thing, and only one of them can live here:
//
// getTestResults — the checks THIS realm recorded. Ours to answer.
// getAssertionResults — evaluates the caller's DECLARATIVE assertion
// grammar (`status eq 200`, `jsonpath(...) exists`)
// through its operator table. That table is the
// API client's vocabulary and does not exist here;
// re-implementing it would be a second semantics
// for one language, which is the drift invariant 3
// forbids.
//
// So the second REFUSES BY NAME rather than returning an empty array. An
// empty array is an answer — "no assertions failed" — and would be a lie.
bru.getTestResults = function () {
if (typeof __tropel_trp_get_test_results !== 'function') {
throw new Error(
'bru.getTestResults is not available here: this realm records no test ' +
'results (tropel TR-478).'
);
}
var raw = __tropel_trp_get_test_results();
try {
return JSON.parse(raw) || [];
} catch (e) {
throw new Error('bru.getTestResults: the host answered with invalid JSON: ' + raw);
}
};
bru.getAssertionResults = function () {
throw new Error(
'bru.getAssertionResults is not available here. It evaluates the API ' +
'client\'s declarative assertion grammar through its operator table, which ' +
'this runtime does not have — and implementing a second copy of that ' +
'grammar is exactly the drift it exists to avoid. Use bru.getTestResults() ' +
'for the checks this realm recorded, or run the assertions on the caller ' +
'side (tropel TR-478).'
);
};
// ── bru.cookies (TR-476) ────────────────────────────────────────────────
//
// The cookie jar surface. This shim shipped WITHOUT one for a long time —
// tropel's own cookie handling lives in reqwest::Jar, load-gen only — so
// every embedder that wanted `bru.cookies` grew its own copy. This is the
// canonical one; an embedder carrying a duplicate should drop it rather
// than keep two implementations of one API (invariant 3).
//
// Every method is guarded on the host bindings and REFUSES BY NAME
// without them, rather than reading as an empty jar — "no cookies" and
// "no jar" must not look the same to a script.
//
// The dual callback/promise form is deliberate: Bruno's cookie methods
// are async, but the jar operations underneath are synchronous. A bare
// call returns the value directly; a trailing function gets
// callback(null, result) AND a settled promise, so `await` works either
// way and neither calling style is wrong.
bru.cookies = (function () {
function need() {
if (typeof __tropel_cookies_all !== 'function') {
throw new Error(
'bru.cookies is not available here: this realm was built without a ' +
'cookie jar. The caller must supply one (tropel TR-476).'
);
}
}
function current() {
return typeof __tropel_cookies_current_url === 'function'
? __tropel_cookies_current_url()
: '';
}
function all() {
need();
// JSON across the boundary, not a marshalled object: a cookie's
// `secure`/`httpOnly` booleans and its expiry survive as
// themselves rather than becoming the strings "true"/"false".
var raw = __tropel_cookies_all(current());
if (!raw) return [];
try {
return JSON.parse(raw) || [];
} catch (e) {
throw new Error('bru.cookies: the host answered with invalid JSON: ' + raw);
}
}
function dual(fn) {
return function () {
var result = fn.apply(null, arguments);
var args = Array.prototype.slice.call(arguments);
if (args.length > 0 && typeof args[args.length - 1] === 'function') {
var cb = args[args.length - 1];
Promise.resolve(result).then(
function (v) { cb(null, v); },
function (e) { cb(e); }
);
return Promise.resolve(result);
}
return result;
};
}
return {
get: dual(function (name) {
var a = all();
for (var i = 0; i < a.length; i++) if (a[i].key === name) return a[i].value;
return undefined;
}),
one: dual(function (name) {
var a = all();
for (var i = 0; i < a.length; i++) if (a[i].key === name) return a[i];
return undefined;
}),
all: dual(function () { return all().slice(); }),
idx: dual(function (i) { return all()[i]; }),
count: dual(function () { return all().length; }),
has: dual(function (name, value) {
var a = all();
for (var i = 0; i < a.length; i++) {
if (a[i].key === name && (arguments.length < 2 || a[i].value === value)) {
return true;
}
}
return false;
}),
add: dual(function (cookieObj) { need(); __tropel_cookies_set(current(), JSON.stringify(cookieObj)); }),
upsert: dual(function (cookieObj) { need(); __tropel_cookies_set(current(), JSON.stringify(cookieObj)); }),
remove: dual(function (name) { need(); __tropel_cookies_delete(current(), name); }),
delete: dual(function (name) { need(); __tropel_cookies_delete(current(), name); }),
clear: dual(function () { need(); __tropel_cookies_clear(current()); }),
jar: function () { return bru.cookies; }
};
})();
// ── bru.runRequest (TR-474) ─────────────────────────────────────────────
//
// Runs ANOTHER request from the caller's collection and returns its
// response. The agent cannot do this itself: the collection, its auth and
// its variables live in the API client, not here. So this is the one
// binding that calls BACK out of the realm, over the host-callback
// channel the caller opened with a `runId`.
//
// Guarded like every other bridge here. Without the channel the binding
// is absent and this REFUSES BY NAME — a load run has no collection to
// re-enter, and silently returning undefined there would read as "the
// request ran and gave nothing back".
bru.runRequest = function (path) {
if (typeof __tropel_trp_run_request !== 'function') {
throw new Error(
'bru.runRequest is not available here: it re-enters the API client\'s ' +
'collection, which this runtime has no access to. It works when the ' +
'caller opened a host-callback channel (tropel TR-474).'
);
}
var raw = __tropel_trp_run_request(String(path));
var parsed;
try {
parsed = JSON.parse(raw);
} catch (e) {
throw new Error('bru.runRequest: the host answered with invalid JSON: ' + raw);
}
// A refusal from the host stays a THROW, not a value. The recursion
// guard, an unknown name and a timeout all arrive this way, and a
// script must not be able to mistake any of them for a response.
if (parsed && parsed.error) {
throw new Error('bru.runRequest: ' + parsed.error);
}
return parsed;
};
// ── req object (pre-request scripts) ───────────────────────────────────
var req = {};
req.setHeader = function (name, value) {
if (typeof __tropel_trp_request_header_set === 'function') {
__tropel_trp_request_header_set(name, String(value));
}
};
req.setMethod = function (method) {
if (typeof __tropel_trp_request_method_set === 'function') {
__tropel_trp_request_method_set(method);
}
};
req.setUrl = function (url) {
if (typeof __tropel_trp_request_url_set === 'function') {
__tropel_trp_request_url_set(url);
}
};
req.setBody = function (body) {
if (typeof __tropel_trp_request_body_set === 'function') {
__tropel_trp_request_body_set(body === undefined ? '' : String(body));
}
};
req.getHeader = function (name) {
if (typeof __tropel_trp_request_header_get === 'function') {
return __tropel_trp_request_header_get(name);
}
return null;
};
req.getBody = function () {
if (typeof __tropel_trp_request_body === 'function') {
return __tropel_trp_request_body();
}
return null;
};
// ── res object (test scripts) ──────────────────────────────────────────
var res = {};
res.getBody = function () {
if (typeof __tropel_trp_response_body === 'function') {
return __tropel_trp_response_body();
}
return null;
};
res.getHeader = function (name) {
if (typeof __tropel_trp_response_header === 'function') {
return __tropel_trp_response_header(name);
}
return null;
};
// Bruno's res.getStatus() returns the numeric status CODE; res.getStatusText()
// returns the text (e.g. "OK"). TROPEL_PARITY_BRUNO.md §0: the old
// implementation returned the text from getStatus() (a silent failure for
// the canonical `expect(res.getStatus()).to.equal(200)` idiom).
res.getStatus = function () {
if (typeof __tropel_trp_response_code === 'function') {
return __tropel_trp_response_code();
}
return 0;
};
res.getStatusText = function () {
if (typeof __tropel_trp_response_status === 'function') {
return __tropel_trp_response_status();
}
return '';
};
res.getResponseTime = function () {
if (typeof __tropel_trp_response_time === 'function') {
return __tropel_trp_response_time();
}
return 0;
};
// ── Install as non-writable globals ─────────────────────────────────────
try {
Object.defineProperty(g, 'bru', { value: bru, writable: false, configurable: false });
Object.defineProperty(g, 'req', { value: req, writable: false, configurable: false });
Object.defineProperty(g, 'res', { value: res, writable: false, configurable: false });
} catch (e) {
// Tolerate double eval: the bindings are already installed read-only.
}
})();