Skip to content

Commit 54c35b5

Browse files
committed
Add some basic tests
1 parent 19f73fe commit 54c35b5

File tree

5 files changed

+133
-33
lines changed

5 files changed

+133
-33
lines changed

pnpm-lock.yaml

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

src/instance.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import { getDisplayInstances } from './tiler'
2+
13
type SetFunc = (name: string, to?: any) => void
24
export class Instance {
35
private static instanceIdCounter = 0
46

7+
public static resetInstanceIdCounter() {
8+
if (Object.keys(inst.instances).length != 0)
9+
throw new Error('inst.instances need to be empty when calling resetInstanceIdCounter!')
10+
this.instanceIdCounter = 0
11+
}
12+
513
static currentReference(name?: string, display?: boolean): Instance {
614
return new Instance(ig, sc, name, display)
715
}
@@ -202,7 +210,7 @@ export class Instance {
202210
}, 100)
203211
})
204212

205-
// s.apply()
213+
s.apply()
206214
return ns
207215
}
208216

@@ -264,7 +272,7 @@ export function injectInstance() {
264272
},
265273
draw() {
266274
this.parent()
267-
if (!Instance.displayInstanceId) return
275+
if (!Instance.displayInstanceId || getDisplayInstances().length <= 1) return
268276
const text = new ig.TextBlock(
269277
sc.fontsystem.font,
270278
`#${inst.instanceId} ${inst.instances[inst.instanceId]?.name ?? 'initializing...'}`,

src/plugin.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default class CCInstanceinator implements PluginClass {
2222
CCInstanceinator.mod = mod
2323
CCInstanceinator.mod.isCCL3 = mod.findAllAssets ? true : false
2424
CCInstanceinator.mod.isCCModPacked = mod.baseDirectory.endsWith('.ccmod/')
25+
if (!CCInstanceinator.mod.isCCL3) Object.assign(mod, { id: CCInstanceinator.mod.name })
2526

2627
this.Instance = Instance
2728

@@ -43,40 +44,45 @@ export default class CCInstanceinator implements PluginClass {
4344
}),
4445
}
4546

46-
let counter = 0
47-
ig.System.inject({
48-
run() {
49-
const instances = Object.values(inst.instances).sort((a, b) => a.id - b.id)
50-
51-
if (instances.length > 0) {
52-
counter++
53-
let nextInst = instances[instances.findIndex(a => a.id == inst.instanceId) + 1]
54-
if (!nextInst) nextInst = instances[0]
55-
56-
nextInst.apply()
57-
}
58-
59-
this.parent()
60-
},
61-
})
62-
47+
// ig.System.inject({
48+
// run() {
49+
// const instances = Object.values(inst.instances).sort((a, b) => a.id - b.id)
50+
// if (instances.length <= 1) return this.parent()
51+
//
52+
// if (instances.length == 6) {
53+
// let nextInst = instances[instances.findIndex(a => a.id == inst.instanceId) + 1]
54+
// if (!nextInst) nextInst = instances[0]
55+
//
56+
// nextInst.apply()
57+
// }
58+
//
59+
// this.parent()
60+
// },
61+
// })
6362
injectInstance()
6463
injectTiling()
65-
}
66-
67-
async poststart() {
68-
this.instances[0] = Instance.currentReference('master')
69-
70-
for (let i = 1; i < 6; i++) {
71-
const instance = await Instance.copy(this.instances[0], 'child')
72-
this.append(instance)
73-
instance.apply()
64+
if (window.crossnode?.options.test) {
65+
import('./tests')
7466
}
7567
}
7668

69+
// async poststart() {
70+
// this.instances[0] = Instance.currentReference('master')
71+
//
72+
// for (let i = 1; i < 6; i++) {
73+
// const instance = await Instance.copy(this.instances[0], 'child')
74+
// this.append(instance)
75+
// instance.apply()
76+
// }
77+
// }
78+
7779
append(instance: Instance) {
7880
this.instances[instance.id] = instance
7981
}
82+
83+
delete(instance: Instance) {
84+
delete this.instances[instance.id]
85+
}
8086
}
8187

8288
declare global {

src/tests.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import CCInstanceinator from './plugin'
2+
3+
const modId = CCInstanceinator.mod.id
4+
5+
ig.System.inject({
6+
run() {
7+
if (crossnode.currentTestId != test1.id) return this.parent()
8+
const instances = Object.values(inst.instances).sort((a, b) => a.id - b.id)
9+
10+
if (test1.startSwapping) {
11+
let nextInst = instances[instances.findIndex(a => a.id == inst.instanceId) + 1]
12+
if (!nextInst) nextInst = instances[0]
13+
14+
nextInst.apply()
15+
}
16+
17+
this.parent()
18+
},
19+
})
20+
21+
crossnode.registerTest({
22+
name: 'initialize master + 1 children',
23+
modId,
24+
skipFrameWait: true,
25+
async setup() {
26+
inst.Instance.resetInstanceIdCounter()
27+
inst.instances[0] = inst.Instance.currentReference('master')
28+
29+
for (let i = 1; i < 2; i++) {
30+
const instance = await inst.Instance.copy(inst.instances[0], 'child')
31+
inst.append(instance)
32+
instance.apply()
33+
}
34+
},
35+
update(_frame) {
36+
this.finish(true)
37+
},
38+
cleanup() {
39+
inst.instances[0].apply()
40+
for (const instance of Object.values(inst.instances)) {
41+
inst.delete(instance)
42+
}
43+
},
44+
})
45+
46+
const test1 = crossnode.registerTest<{
47+
frameCountRecord: Record<number, number>
48+
instanceCount: number
49+
startSwapping: boolean
50+
}>({
51+
name: 'swap update master + 5 children',
52+
modId,
53+
skipFrameWait: true,
54+
55+
frameCountRecord: {},
56+
instanceCount: 4,
57+
startSwapping: false,
58+
async setup() {
59+
inst.Instance.resetInstanceIdCounter()
60+
inst.instances[0] = inst.Instance.currentReference('master')
61+
62+
for (let i = 1; i < this.instanceCount; i++) {
63+
const instance = await inst.Instance.copy(inst.instances[0], 'child')
64+
inst.append(instance)
65+
}
66+
this.startSwapping = true
67+
},
68+
update(frame) {
69+
this.frameCountRecord[inst.instanceId] ??= 0
70+
this.frameCountRecord[inst.instanceId]++
71+
72+
const multi = 6
73+
if (frame + 1 >= this.instanceCount * multi) {
74+
for (let i = 0; i < this.instanceCount; i++) {
75+
if (this.frameCountRecord[i] != multi)
76+
return this.finish(
77+
false,
78+
`Mismatch at instance ${i}. Full object: ${JSON.stringify(this.frameCountRecord)}`
79+
)
80+
}
81+
82+
this.finish(true)
83+
}
84+
},
85+
cleanup() {},
86+
})

src/tiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ declare global {
66
}
77
}
88

9-
function getDisplayInstances() {
9+
export function getDisplayInstances() {
1010
return Object.values(inst.instances).filter(i => i.display)
1111
}
1212

0 commit comments

Comments
 (0)