167 lines
4.8 KiB
Plaintext
167 lines
4.8 KiB
Plaintext
// Discord SDK Integration Test (Actor-based)
|
|
// This test runs the full Discord integration flow with callbacks
|
|
// It sends results back to the test runner via $_.parent
|
|
//
|
|
// To run standalone: cell discord_integration
|
|
// As part of test suite: cell test
|
|
|
|
var discord = use('discord')
|
|
var time = use('time')
|
|
|
|
// Test application ID (Tangle Tart)
|
|
def TEST_APP_ID = "1446585686789586975"
|
|
def CALLBACK_INTERVAL = 50
|
|
def TEST_TIMEOUT = 10000 // 10 seconds
|
|
|
|
var test_start = time.number()
|
|
var test_passed = false
|
|
var test_error = null
|
|
var status_received = false
|
|
var connected = false
|
|
|
|
// Report test result to parent (test runner)
|
|
function report_result(passed, error) {
|
|
if ($_.parent) {
|
|
$_.send($_.parent, {
|
|
type: "test_result",
|
|
passed: passed,
|
|
error: error
|
|
})
|
|
} else {
|
|
// Running standalone
|
|
if (passed) {
|
|
log.console("TEST PASSED")
|
|
} else {
|
|
log.console(`TEST FAILED: ${error}`)
|
|
}
|
|
}
|
|
$_.stop()
|
|
}
|
|
|
|
// Status change handler
|
|
function on_status(status, error, detail) {
|
|
log.console(`[Test] Status changed: ${status} (error: ${error}, detail: ${detail})`)
|
|
status_received = true
|
|
|
|
if (status == "ready") {
|
|
connected = true
|
|
// Run post-connection tests
|
|
run_connected_tests()
|
|
} else if (error != "none") {
|
|
// Connection error - this is expected if Discord isn't running
|
|
log.console(`[Test] Connection error (expected if Discord app not running): ${error}`)
|
|
// Still pass the test - we verified the callback system works
|
|
report_result(true, null)
|
|
}
|
|
}
|
|
|
|
// Log handler
|
|
function on_log(msg, severity) {
|
|
log.console(`[Discord ${severity}] ${msg}`)
|
|
}
|
|
|
|
// Tests that run after connection
|
|
function run_connected_tests() {
|
|
log.console("[Test] Running connected tests...")
|
|
|
|
try {
|
|
// Test get_current_user
|
|
var user = discord.get_current_user()
|
|
if (user) {
|
|
log.console(`[Test] Current user: ${user.display_name} (@${user.username})`)
|
|
log.console(`[Test] User ID: ${user.id}`)
|
|
} else {
|
|
log.console("[Test] No user (not authenticated)")
|
|
}
|
|
|
|
// Test get_relationships
|
|
var rels = discord.get_relationships()
|
|
log.console(`[Test] Relationships: ${rels.length}`)
|
|
|
|
// Test get_friends_count
|
|
var count = discord.get_friends_count()
|
|
log.console(`[Test] Friends count: ${count}`)
|
|
|
|
// Test update_rich_presence
|
|
discord.update_rich_presence({
|
|
type: "playing",
|
|
state: "Running Tests",
|
|
details: "Discord SDK Integration Test"
|
|
}, function(success, error) {
|
|
if (success) {
|
|
log.console("[Test] Rich presence updated")
|
|
} else {
|
|
log.console(`[Test] Rich presence failed: ${error}`)
|
|
}
|
|
})
|
|
|
|
// All tests passed
|
|
test_passed = true
|
|
|
|
// Give time for presence update callback
|
|
$_.delay(function() {
|
|
discord.clear_rich_presence()
|
|
discord.shutdown()
|
|
report_result(true, null)
|
|
}, 1000)
|
|
|
|
} catch (e) {
|
|
report_result(false, e.toString())
|
|
}
|
|
}
|
|
|
|
// Callback tick
|
|
function tick() {
|
|
discord.run_callbacks()
|
|
|
|
// Check timeout
|
|
var elapsed = time.number() - test_start
|
|
if (elapsed > TEST_TIMEOUT) {
|
|
if (!status_received) {
|
|
// No status callback received - SDK might not be working
|
|
report_result(false, "Timeout: No status callback received")
|
|
} else if (!connected) {
|
|
// Status received but not connected - Discord app probably not running
|
|
// This is acceptable for CI environments
|
|
log.console("[Test] Timeout waiting for connection (Discord app may not be running)")
|
|
discord.shutdown()
|
|
report_result(true, null) // Pass - callbacks work, just no Discord app
|
|
}
|
|
return
|
|
}
|
|
|
|
$_.delay(tick, CALLBACK_INTERVAL)
|
|
}
|
|
|
|
// Main test
|
|
function main() {
|
|
log.console("=== Discord SDK Integration Test ===")
|
|
log.console(`Application ID: ${TEST_APP_ID}`)
|
|
|
|
try {
|
|
// Initialize
|
|
var init_result = discord.init(TEST_APP_ID)
|
|
if (!init_result) {
|
|
report_result(false, "discord.init failed")
|
|
return
|
|
}
|
|
log.console("[Test] Discord initialized")
|
|
|
|
// Set up callbacks
|
|
discord.on_status_changed(on_status)
|
|
discord.on_log(on_log, "info")
|
|
|
|
// Start callback loop
|
|
tick()
|
|
|
|
// Try to connect (will trigger status callbacks)
|
|
discord.connect()
|
|
log.console("[Test] Connection initiated, waiting for callbacks...")
|
|
|
|
} catch (e) {
|
|
report_result(false, e.toString())
|
|
}
|
|
}
|
|
|
|
main()
|