import assert from "node:assert/strict" import {readGreComment,writeGreComment} from "./gre-comment.js" import {canonicalGreSnapshot,planGreCreate,planGreRestore} from "./entity-snapshots.js" import {mapGreLive} from "./gre-live.js" const comment="Тестовый туннель\nВторая строка",peer="198.18.255.250/30" const raw=writeGreComment(comment,peer) assert.deepEqual(readGreComment(raw),{comment,remoteInnerIp:peer}) assert.equal(writeGreComment(raw,peer),raw) assert.deepEqual(readGreComment("gateway=10.0.0.2/30"),{comment:"gateway=10.0.0.2/30",remoteInnerIp:"10.0.0.2/30"}) assert.equal(readGreComment("note\n[mmapp-gre-peer:v1:bad]").comment,"note\n[mmapp-gre-peer:v1:bad]") const desired=canonicalGreSnapshot({tunnels:[{name:"gre-test",localInnerIp:"198.18.255.249/30",remoteInnerIp:peer,comment}]}) const create=planGreCreate(desired.tunnels[0]);assert.deepEqual(create.map(x=>x.path),["/interface/gre","/ip/address"]) const native={".id":"*3",name:"gre-test","local-address":"194.226.121.150","remote-address":"104.165.244.7",comment:(create[0] as {body:Record}).body.comment,mtu:"1400",disabled:"false",running:"true"} const server={id:1} as Parameters[0] const live=mapGreLive(server,[native],[{".id":"*4",address:"198.18.255.249/30",interface:"gre-test"}]) assert.equal(live.tunnels[0].remoteInnerIp,peer);assert.equal(live.tunnels[0].comment,comment);assert.equal(live.snapshot.tunnels[0].remoteInnerIp,peer) const restored=planGreRestore(live.snapshot,{gre:live.gre,addrs:live.addrs});const iface=restored.find(x=>x.op==="patch"&&x.path==="/interface/gre/*3") as {body:Record};assert(iface);assert.equal(readGreComment(iface.body.comment).remoteInnerIp,peer) assert(restored.every(x=>!x.path.startsWith("/ip/route"))) const clearSnapshot=canonicalGreSnapshot({tunnels:[{...live.snapshot.tunnels[0],remoteInnerIp:""}]}) const clearedRestore=planGreRestore(clearSnapshot,{gre:live.gre,addrs:live.addrs}).find(x=>x.op==="patch"&&x.path==="/interface/gre/*3") as {body:Record} assert.equal(readGreComment(clearedRestore.body.comment).remoteInnerIp,"") const legacy=canonicalGreSnapshot({tunnels:[{name:"legacy",comment:"gateway=10.0.0.2/30",remoteInnerIp:""}]});assert.equal(readGreComment((planGreCreate(legacy.tunnels[0])[0] as {body:Record}).body.comment).remoteInnerIp,"10.0.0.2/30") const cleared=writeGreComment("gateway=10.0.0.2/30","");assert.equal(readGreComment(cleared).remoteInnerIp,"") const clearPlan=planGreCreate(canonicalGreSnapshot({tunnels:[{name:"clear",comment:cleared,remoteInnerIp:""}]}).tunnels[0]);assert.equal(readGreComment((clearPlan[0] as {body:Record}).body.comment).remoteInnerIp,"") assert.equal(readGreComment(writeGreComment(raw,"")).comment,comment) console.log("GRE peer metadata: native/read/restore/legacy/clear, no route operations")