Skip to content

Commit 30d729a

Browse files
committed
mocks for network address obtaining
1 parent 54df33a commit 30d729a

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

build/scripts/main.js

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/src/env.ts

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,75 @@ class Fi {
5454
}
5555
}
5656

57-
Object.assign(globalThis, {Pattern, ObjectIntMap, Seq, Fi});
57+
const Collections = {
58+
list<T>(e:Enumeration<T>){
59+
return new ArrayList(e.items);
60+
}
61+
};
62+
class Enumeration<T> {
63+
constructor(public items:T[]){}
64+
}
65+
class ArrayList<T> {
66+
constructor(public items:T[]){}
67+
stream(){
68+
return new Stream(this.items);
69+
}
70+
}
71+
class NetworkInterface {
72+
constructor(
73+
public interfaceAddresses: InterfaceAddress[],
74+
public loopback = false,
75+
public up = true,
76+
){}
77+
getInterfaceAddresses(){ return new ArrayList(this.interfaceAddresses); }
78+
isUp(){ return this.up; }
79+
isLoopback(){ return this.loopback; }
80+
static getNetworkInterfaces(){
81+
return new Enumeration([
82+
new NetworkInterface([new InterfaceAddress(new Inet6Address("0:0:0:0:0:0:0:1")), new InterfaceAddress(new Inet4Address("127.0.0.1"))], true), //loopback
83+
new NetworkInterface([new InterfaceAddress(new Inet6Address("fe80:0:0:0:216:3eff:feaa:b35c")), new InterfaceAddress(new Inet4Address("1.2.3.4"))]), //eth0
84+
]);
85+
}
86+
}
87+
class Stream<T> {
88+
iterator: IteratorObject<T, undefined>;
89+
constructor(items:T[]){
90+
this.iterator = items.values();
91+
}
92+
map<U>(operation:(item:T) => U){
93+
(this as never as Stream<U>).iterator = this.iterator.map(operation);
94+
return this as never as Stream<U>;
95+
}
96+
filter(operation:(item:T) => boolean){
97+
this.iterator = this.iterator.filter(operation);
98+
return this;
99+
}
100+
findFirst(){
101+
return new Optional<T>(this.iterator.next()?.value ?? null);
102+
}
103+
}
104+
class Optional<T> {
105+
constructor(public item:T | null){}
106+
orElse<U>(value:U){
107+
return this.item ?? value;
108+
}
109+
}
110+
class InterfaceAddress {
111+
constructor(public address: InetAddress){}
112+
getAddress(){ return this.address; }
113+
}
114+
class InetAddress {
115+
constructor(public hostAddress: string){}
116+
getHostAddress(){ return this.hostAddress; }
117+
}
118+
class Inet4Address extends InetAddress {}
119+
class Inet6Address extends InetAddress {}
120+
121+
const Packages = {
122+
java: {
123+
net: { NetworkInterface, Inet4Address },
124+
util: { Collections }
125+
}
126+
};
127+
128+
Object.assign(globalThis, {Pattern, ObjectIntMap, Seq, Fi, Packages});

spec/src/utils.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { capitalizeText } from "../../build/scripts/funcs.js";
1+
import { capitalizeText, getIPAddress } from "../../build/scripts/funcs.js";
22
import { formatTime } from "../../build/scripts/utils.js";
33
import { maxTime } from "../../build/scripts/globals.js";
44

@@ -14,6 +14,12 @@ describe("capitalizeText", () => {
1414
});
1515
});
1616

17+
describe("getIPAddress", () => {
18+
it("should return the correct address from the available network interfaces", () => {
19+
expect(getIPAddress()).toEqual("1.2.3.4");
20+
});
21+
});
22+
1723
describe("formatTime", () => {
1824
it("should work for normal times", () => {
1925
expect(formatTime(1_000)).toEqual("1 second");

spec/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"types": ["node", "jasmine"],
77
"composite": false,
88
"declaration": false,
9+
"lib": ["ESNext"],
910
},
1011
"references": [{"path": ".."}],
1112
"include": [

0 commit comments

Comments
 (0)