Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/test/externalTests/time.js
2155 views
1
const assert = require('assert')
2
const { once } = require('../../lib/promise_utils')
3
4
module.exports = () => async (bot) => {
5
// Test time properties and ranges
6
const timeProps = {
7
doDaylightCycle: 'boolean',
8
bigTime: 'bigint',
9
time: 'number',
10
timeOfDay: 'number',
11
day: 'number',
12
isDay: 'boolean',
13
moonPhase: 'number',
14
bigAge: 'bigint',
15
age: 'number'
16
}
17
18
// Verify all properties exist and have correct types
19
Object.entries(timeProps).forEach(([prop, type]) => {
20
assert.strictEqual(typeof bot.time[prop], type, `Property ${prop} should be of type ${type}`)
21
})
22
23
// Verify ranges
24
assert(bot.time.timeOfDay >= 0 && bot.time.timeOfDay < 24000, 'timeOfDay should be between 0 and 24000')
25
assert(bot.time.moonPhase >= 0 && bot.time.moonPhase < 8, 'moonPhase should be between 0 and 7')
26
assert(bot.time.day >= 0, 'day should be non-negative')
27
assert(bot.time.age >= 0, 'age should be non-negative')
28
assert(bot.time.bigAge >= 0n, 'bigAge should be non-negative')
29
30
// Helper functions
31
const isTimeClose = (current, target) => Math.abs(current - target) < 510
32
const isTimeInRange = (current, start, end) => start <= end ? current >= start && current <= end : current >= start || current <= end
33
const waitForTime = async () => {
34
await once(bot, 'time')
35
await bot.test.wait(200)
36
}
37
38
// Test time transitions
39
const timeTests = [
40
{ time: 18000, name: 'midnight', isDay: false },
41
{ time: 6000, name: 'noon', isDay: true },
42
{ time: 12000, name: 'sunset', isDay: true },
43
{ time: 0, name: 'sunrise', isDay: true }
44
]
45
46
for (const test of timeTests) {
47
bot.test.sayEverywhere(`/time set ${test.time}`)
48
await waitForTime()
49
assert(isTimeClose(bot.time.timeOfDay, test.time), `Expected time to be close to ${test.time}, got ${bot.time.timeOfDay}`)
50
assert.strictEqual(bot.time.isDay, test.isDay, `${test.name} should be ${test.isDay ? 'day' : 'night'}`)
51
}
52
53
// Test day and moon phase progression
54
const currentDay = bot.time.day
55
const currentPhase = bot.time.moonPhase
56
bot.test.sayEverywhere('/time add 24000')
57
await waitForTime()
58
assert(bot.time.day >= currentDay + 1, `Expected day to be at least ${currentDay + 1}, got ${bot.time.day}`)
59
assert.notStrictEqual(bot.time.moonPhase, currentPhase, 'Moon phase should change after a full day')
60
61
// Test daylight cycle
62
const originalDaylightCycle = bot.time.doDaylightCycle
63
bot.test.sayEverywhere('/gamerule doDaylightCycle false')
64
await waitForTime()
65
assert.strictEqual(bot.time.doDaylightCycle, false)
66
67
bot.test.sayEverywhere(`/gamerule doDaylightCycle ${originalDaylightCycle}`)
68
await waitForTime()
69
assert.strictEqual(bot.time.doDaylightCycle, originalDaylightCycle)
70
71
// Test day/night transitions
72
const dayNightTests = [
73
{ command: 'day', range: [0, 12000], isDay: true },
74
{ command: 'night', range: [12000, 24000], isDay: false }
75
]
76
77
for (const test of dayNightTests) {
78
bot.test.sayEverywhere(`/time set ${test.command}`)
79
await waitForTime()
80
assert(isTimeInRange(bot.time.timeOfDay, test.range[0], test.range[1]), `Time should be in ${test.command} range`)
81
assert.strictEqual(bot.time.isDay, test.isDay, `${test.command} should be ${test.isDay ? 'day' : 'night'}`)
82
}
83
}
84
85