// Source-level UI template contract test; does not execute the Rust proxy. 'use strict'; const assert = require('node:assert/strict'); const fs = require('node:fs'); const vm = require('node:vm'); const path = require('node:path'); const root = path.resolve(__dirname, '..'); const ui = fs.readFileSync(path.join(root, 'src/admin/web/cache-rules.js'), 'utf8'); const sandbox = { window: {}, t: key => key }; vm.runInNewContext(ui, sandbox, { filename: 'cache-rules.js' }); const tpl = sandbox.window.rustProxyCacheRules.TEMPLATES.publiccookies; assert.equal(typeof tpl, 'function', 'new template is registered'); const rules = JSON.parse(JSON.stringify(tpl())); assert.equal(rules.length, 1); const rule = rules[0]; assert.equal(rule.enabled, false, 'operator must opt in explicitly'); assert.equal(rule.stage, 'request'); assert.equal(rule.match, 'all'); assert.equal(rule.stop, true); assert.deepEqual(rule.actions, [{ type: 'strip_cookies', keep: [] }, { type: 'lookup' }]); const method = rule.conditions.find(c => c.field === 'method'); assert.deepEqual(method.values, ['GET', 'HEAD']); const cookie = rule.conditions.find(c => c.name === 'Cookie'); assert.equal(cookie.operator, 'exists'); const auth = rule.conditions.find(c => c.name === 'Authorization'); assert.equal(auth.operator, 'exists'); assert.equal(auth.negate, true); const restricted = rule.conditions.find(c => c.field === 'path'); assert.equal(restricted.negate, true); const rex = new RegExp(restricted.values[0]); for (const path of ['/admin', '/admin/orders', '/api/public', '/konto', '/login?next=/']) assert.ok(rex.test(path), `protect ${path}`); for (const path of ['/', '/rezerwa/15', '/catalog', '/stories/article']) assert.ok(!rex.test(path), `public ${path}`); assert.match(ui, /filter\(key => scope === "vhost" \|\| key !== "publiccookies"\)/, 'dangerous opt-in template is unavailable in global editor'); const vcl = fs.readFileSync(path.join(root, 'examples/vcl/public-vhost-ignore-cookies.vcl'), 'utf8'); assert.ok(vcl.includes(restricted.values[0]), 'VCL and UI use the same excluded paths'); for (const line of ['unset req.http.Cookie;', 'return(hash);', 'req.http.Cookie', '!req.http.Authorization']) assert.ok(vcl.includes(line), `${line} is in example VCL`); console.log('Public-cookie graphical template/VCL contract: OK');