import { newBurpaState, CBurpastateWrapper, BurpaStateLens } from "../src/Engine/burpastate.js" import { HostName } from "../src/Independents/DNS/HostName.js" import { Resolve } from "../src/Independents/DNS/Resolve.js"; describe("burpanet", function () { it("should initialize burpa state", async () => { let bs = newBurpaState(); bs.persistent.localGateway = "gw55123"; bs.persistent.localHost = "vg66123"; let cbs = new CBurpastateWrapper(bs); cbs.addGateway_Old(bs.persistent.localGateway); cbs.addVagrant_Old(bs.persistent.localHost, {}); expect(bs.persistent.processType).toEqual("nodejs"); expect(bs.persistent.localHost).toEqual("vg66123"); expect(bs.persistent.localGateway).toEqual("gw55123"); expect(cbs.localGateway.type).toEqual("gateway"); expect(cbs.localHost.type).toEqual("vagrant"); }); it("should parse host name", async () => { let hn = new HostName("a.b.c.heads.com"); expect(hn.reconstructed).toEqual("a.b.c.heads.com"); expect(hn.components.length).toEqual(5); hn = new HostName("a-b-c.heads.com"); expect(hn.reconstructed).toEqual("a-b-c.heads.com"); expect(hn.components.length).toEqual(5); expect(hn.port).toEqual("80"); hn = new HostName("a-b-c.heads.com:8000"); expect(hn.reconstructed).toEqual("a-b-c.heads.com:8000"); expect(hn.port).toEqual("8000"); expect(hn.components.length).toEqual(5); }); it("should check downstream test", async () => { let root = new HostName("heads.com"); let child = new HostName("a.b.c.heads.com"); let child2 = new HostName("heads.com"); let child3 = new HostName("net"); let child4 = new HostName("aheads.com"); expect(child.isDownstreamOf(root)).toBeTruthy(); expect(child2.isDownstreamOf(root)).toBeTruthy(); expect(child3.isDownstreamOf(root)).toBeFalsy(); expect(child4.isDownstreamOf(root)).toBeFalsy(); }); it("should be able to extract root host", async () => { let a = new HostName("a.b.c.gw.heads.com"); let b = new HostName("gw.heads.com"); let c = a.getRoot(b, 1); expect(c.name).toEqual("c.gw.heads.com"); let d = a.getRoot(b, 2); expect(d.name).toEqual("b.c.gw.heads.com"); let e = a.getRoot(b, 0); expect(e.name).toEqual("gw.heads.com"); let f = a.getRoot(b, 3); expect(f.name).toEqual("a.b.c.gw.heads.com"); }); it("should resolve root", async () => { let hn = new HostName("a.b.c.heads.com"); let gw = new HostName("gw.heads.com"); let gwr = Resolve.fromHostName(gw); gwr.type = "gateway"; gwr.parent!.type = "root-host"; let unresolved = Resolve.fromHostName(hn); let x = unresolved.learnFrom(gwr); expect(x.debugStr).toEqual("a?.b?.c?.heads(root-host).com(domain)"); let gw2 = Resolve.fromHostName(new HostName("x-y-z-gw.heads.com")); expect(gw2.debugStr).toEqual("x?-y?-z?-gw?.heads?.com?"); let root = Resolve.fromHostName(new HostName("heads.com")); root.type = "root-host"; gw2 = gw2.learnFrom(root); expect(gw2.debugStr).toEqual("x?-y?-z?-gw?.heads(root-host).com(domain)"); gw2 = gw2.learnFrom(gwr); expect(gw2.debugStr).toEqual("x(gateway)-y(gateway)-z(gateway)-gw(gateway).heads(root-host).com(domain)"); let foo = Resolve.fromHostName(new HostName("foo-bar.heads.com")); let bar = Resolve.fromHostName(new HostName("bar.heads.com")); bar.type = "vagrant"; bar.gateway = "vaxholm"; foo = foo.learnFrom(bar); foo = foo.learnFrom(root); foo = foo.learnFrom(gwr); expect(foo.debugStr).toEqual("foo(vagrant)-bar(vagrant@vaxholm).heads(root-host).com(domain)"); let bar2 = Resolve.fromHostName(new HostName("bar.heads.com")); bar2 = bar2.learnFrom(foo); expect(bar2.type).toEqual("vagrant"); expect(bar2.gateway).toEqual("vaxholm"); }); // it("should find common root", async () => { // let a = new HostName("a1.b1.c1.d.heads.com"); // let b = new HostName("b2.c2.d.heads.com"); // let c1 = HostName.commonRoot(a, b); // expect(c1.name).toEqual("d.heads.com"); // let c2 = HostName.commonRoot(b, a); // expect(c2.name).toEqual("d.heads.com"); // }); it("should identify gateways", async () => { let root = Resolve.fromHostName(new HostName("heads.com")); root.type = "root-host"; expect(root.debugStr).toEqual("heads(root-host).com(domain)"); let gw = Resolve.fromHostName(new HostName("gw.heads.com")); gw = gw.learnFrom(root); expect(gw.host).toEqual("gw.heads.com"); expect(gw.debugStr).toEqual("gw?.heads(root-host).com(domain)"); gw.type = "gateway" expect(gw.debugStr).toEqual("gw(gateway).heads(root-host).com(domain)"); let a = Resolve.fromHostName(new HostName("a-b-c-gw.heads.com")); expect(a.reconstructed).toEqual("a-b-c-gw.heads.com"); a = a.learnFrom(gw); a = a.learnFrom(root); expect(a.debugStr).toEqual("a(gateway)-b(gateway)-c(gateway)-gw(gateway).heads(root-host).com(domain)"); let b = Resolve.fromHostName(new HostName("b-c-gw.heads.com")); a = a.learnFrom(b); }); });