Skip to content

Commit 3b71acc

Browse files
committed
mocks for network address obtaining
1 parent 54df33a commit 3b71acc

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-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: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,73 @@ 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+
static getNetworkInterfaces(){
79+
return [
80+
new NetworkInterface([new InterfaceAddress(new Inet6Address("0:0:0:0:0:0:0:1")), new InterfaceAddress(new Inet4Address("127.0.0.1"))]), //loopback
81+
new NetworkInterface([new InterfaceAddress(new Inet6Address("fe80:0:0:0:216:3eff:feaa:b35c")), new InterfaceAddress(new Inet4Address("1.2.3.4"))]), //eth0
82+
];
83+
}
84+
}
85+
class Stream<T> {
86+
iterator: IteratorObject<T, undefined>;
87+
constructor(items:T[]){
88+
this.iterator = items.values();
89+
}
90+
map<U>(operation:(item:T) => U){
91+
(this as never as Stream<U>).iterator = this.iterator.map(operation);
92+
return this as never as Stream<U>;
93+
}
94+
filter(operation:(item:T) => boolean){
95+
this.iterator = this.iterator.filter(operation);
96+
return this;
97+
}
98+
findFirst(){
99+
return new Optional<T>(this.iterator.next()?.value ?? null);
100+
}
101+
}
102+
class Optional<T> {
103+
constructor(public item:T | null){}
104+
orElse<U>(value:U){
105+
return this.item ?? value;
106+
}
107+
}
108+
class InterfaceAddress {
109+
constructor(public address: InetAddress){}
110+
getAddress(){ return this.address; }
111+
}
112+
class InetAddress {
113+
constructor(public hostAddress: string){}
114+
getHostAddress(){ return this.hostAddress; }
115+
}
116+
class Inet4Address extends InetAddress {}
117+
class Inet6Address extends InetAddress {}
118+
119+
const Packages = {
120+
java: {
121+
net: { NetworkInterface, Inet4Address },
122+
util: { Collections }
123+
}
124+
};
125+
126+
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)