Skip to content

Commit aac70b4

Browse files
committed
Task tests
1 parent a786dfe commit aac70b4

File tree

9 files changed

+100
-14
lines changed

9 files changed

+100
-14
lines changed

common/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ val kotlinxCoroutinesVersion: String by project
2323
val discordIPCVersion: String by project
2424
val fuelVersion: String by project
2525
val resultVersion: String by project
26+
val mockitoKotlin: String by project
27+
val mockitoInline: String by project
2628

2729
base.archivesName = "${base.archivesName.get()}-api"
2830

@@ -58,6 +60,8 @@ dependencies {
5860
// Baritone
5961
modImplementation("baritone-api:baritone-unoptimized-fabric:1.10.2") { isTransitive = false }
6062
testImplementation(kotlin("test"))
63+
testImplementation("org.mockito.kotlin:mockito-kotlin:$mockitoKotlin")
64+
testImplementation("org.mockito:mockito-inline:$mockitoInline")
6165
}
6266

6367
tasks {

common/src/main/kotlin/com/lambda/module/modules/network/PacketLimiter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ object PacketLimiter : Module(
3535
) {
3636
private var packetQueue = LimitedDecayQueue<PacketEvent.Send.Pre>(99, 1000)
3737
private val limit by setting("Limit", 99, 1..100, 1, "The maximum amount of packets to send per given time interval", unit = " packets")
38-
.onValueChange { _, to -> packetQueue.setMaxSize(to) }
38+
.onValueChange { _, to -> packetQueue.setSizeLimit(to) }
3939

4040
private val interval by setting("Duration", 1000L, 1L..1000L, 50L, "The interval / duration in milliseconds to limit packets for", unit = " ms")
4141
.onValueChange { _, to -> packetQueue.setDecayTime(to) }

common/src/main/kotlin/com/lambda/task/Task.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ typealias TaskGeneratorUnit<R> = SafeContext.(R) -> Unit
3838

3939
abstract class Task<Result> : Nameable, Muteable {
4040
private var parent: Task<*>? = null
41-
private val subTasks = mutableListOf<Task<*>>()
42-
private var state = State.INIT
41+
val subTasks = mutableListOf<Task<*>>()
42+
var state = State.INIT
4343
override val isMuted: Boolean get() = state == State.PAUSED || state == State.INIT
4444
var age = 0
4545
private val depth: Int get() = parent?.depth?.plus(1) ?: 0

common/src/main/kotlin/com/lambda/task/tasks/OpenContainer.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class OpenContainer @Ta5kBuilder constructor(
3939
private val interact: InteractionConfig = TaskFlowModule.interact,
4040
private val sides: Set<Direction> = Direction.entries.toSet(),
4141
) : Task<ScreenHandler>() {
42-
override val name get() = "${state.description(inScope)} at ${blockPos.toShortString()}"
42+
override val name get() = "${containerState.description(inScope)} at ${blockPos.toShortString()}"
4343

4444
private var screenHandler: ScreenHandler? = null
45-
private var state = State.SCOPING
45+
private var containerState = State.SCOPING
4646
private var inScope = 0
4747

4848
enum class State {
@@ -57,39 +57,39 @@ class OpenContainer @Ta5kBuilder constructor(
5757

5858
init {
5959
listen<InventoryEvent.Open> {
60-
if (state != State.OPENING) return@listen
60+
if (containerState != State.OPENING) return@listen
6161

6262
screenHandler = it.screenHandler
63-
state = State.SLOT_LOADING
63+
containerState = State.SLOT_LOADING
6464

6565
if (!waitForSlotLoad) success(it.screenHandler)
6666
}
6767

6868
listen<InventoryEvent.Close> {
6969
if (screenHandler != it.screenHandler) return@listen
7070

71-
state = State.SCOPING
71+
containerState = State.SCOPING
7272
screenHandler = null
7373
}
7474

7575
listen<InventoryEvent.FullUpdate> {
76-
if (state != State.SLOT_LOADING) return@listen
76+
if (containerState != State.SLOT_LOADING) return@listen
7777

7878
screenHandler?.let {
7979
success(it)
8080
}
8181
}
8282

8383
listen<TickEvent.Pre> {
84-
if (state != State.SCOPING) return@listen
84+
if (containerState != State.SCOPING) return@listen
8585

8686
val target = lookAtBlock(blockPos, sides, config = interact)
8787
if (rotate && !target.requestBy(rotation).done) return@listen
8888

8989
val hitResult = target.hit?.hitIfValid()?.blockResult ?: return@listen
9090
interaction.interactBlock(player, Hand.MAIN_HAND, hitResult)
9191

92-
state = State.OPENING
92+
containerState = State.OPENING
9393
}
9494
}
9595
}

common/src/main/kotlin/com/lambda/util/collections/LimitedDecayQueue.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class LimitedDecayQueue<E>(
110110
*
111111
* @param newSize The new maximum size for the queue. Must be a non-negative integer.
112112
*/
113-
fun setMaxSize(newSize: Int) {
113+
fun setSizeLimit(newSize: Int) {
114114
sizeLimit = newSize
115115
cleanUp()
116116

common/src/test/kotlin/LimitedDecayQueueTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class LimitedDecayQueueTest {
135135
queue.add("Element2")
136136
queue.add("Element3")
137137

138-
queue.setMaxSize(2) // Reduce size limit to 2
138+
queue.setSizeLimit(2) // Reduce size limit to 2
139139

140140
assertEquals(2, queue.size)
141141
}

common/src/test/kotlin/TaskTest.kt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import com.lambda.context.ClientContext
2+
import com.lambda.context.SafeContext
3+
import com.lambda.task.RootTask
4+
import com.lambda.task.RootTask.run
5+
import com.lambda.task.Task
6+
import org.junit.jupiter.api.AfterEach
7+
import org.junit.jupiter.api.BeforeEach
8+
import org.junit.jupiter.api.Test
9+
import org.mockito.Mock
10+
import org.mockito.MockedConstruction
11+
import org.mockito.Mockito
12+
import org.mockito.Mockito.`when`
13+
import org.mockito.MockitoAnnotations
14+
import kotlin.test.assertEquals
15+
import kotlin.test.assertTrue
16+
17+
/*
18+
* Copyright 2025 Lambda
19+
*
20+
* This program is free software: you can redistribute it and/or modify
21+
* it under the terms of the GNU General Public License as published by
22+
* the Free Software Foundation, either version 3 of the License, or
23+
* (at your option) any later version.
24+
*
25+
* This program is distributed in the hope that it will be useful,
26+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
27+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28+
* GNU General Public License for more details.
29+
*
30+
* You should have received a copy of the GNU General Public License
31+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
32+
*/
33+
34+
35+
class TaskTest {
36+
37+
@Mock
38+
private lateinit var mockSafeContext: SafeContext
39+
40+
private lateinit var clientContextMock: MockedConstruction<ClientContext>
41+
42+
class TestTask(private val i: Int = 0) : Task<Int>() {
43+
override val name get() = "TestTask of $i"
44+
45+
override fun SafeContext.onStart() {
46+
success(i + 1)
47+
}
48+
49+
override fun SafeContext.onCancel() {
50+
success(i)
51+
}
52+
}
53+
54+
@BeforeEach
55+
fun setUp() {
56+
MockitoAnnotations.openMocks(this)
57+
clientContextMock = Mockito.mockConstruction(ClientContext::class.java) { mock, _ ->
58+
`when`(mock.toSafe()).thenReturn(mockSafeContext)
59+
}
60+
}
61+
62+
@AfterEach
63+
fun tearDown() {
64+
clientContextMock.close()
65+
RootTask.clear()
66+
}
67+
68+
@Test
69+
fun `test task`() {
70+
val task = TestTask(5)
71+
72+
assertEquals(task.name, "TestTask of 5")
73+
74+
task.finally { result ->
75+
assertEquals(result, 6)
76+
assertEquals(task.state, Task.State.COMPLETED)
77+
assertTrue(task.isCompleted)
78+
}.run()
79+
}
80+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import kotlin.test.assertEquals
3232
* along with this program. If not, see <http://www.gnu.org/licenses/>.
3333
*/
3434

35-
class Vec3Test {
35+
class Vec3dTest {
3636

3737
@Test
3838
fun `test dist with another Vec3d`() {

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ baritoneVersion=1.10.2
3434
discordIPCVersion=8edf2dbeda
3535
fuelVersion=2.3.1
3636
resultVersion=5.6.0
37+
mockitoKotlin=5.4.0
38+
mockitoInline=5.2.0
3739

3840
# Fabric https://fabricmc.net/develop/
3941
fabricLoaderVersion=0.16.9

0 commit comments

Comments
 (0)