141 lines
4.2 KiB
JavaScript
141 lines
4.2 KiB
JavaScript
import fs from 'fs';
|
|
import assert from 'assert';
|
|
import { sendNotification, CONFIG } from './index.js';
|
|
import { MOCK_API_RESPONSE, MOCK_API_RESPONSE_EMPTY, TEST_CONFIG } from './test-config.js';
|
|
|
|
// Mock fetch function
|
|
let mockFetchResponse = null;
|
|
const originalFetch = global.fetch;
|
|
|
|
function mockFetch(url, options) {
|
|
if (mockFetchResponse) {
|
|
return Promise.resolve({
|
|
ok: true,
|
|
json: () => Promise.resolve(mockFetchResponse),
|
|
status: 200,
|
|
statusText: 'OK'
|
|
});
|
|
}
|
|
return originalFetch(url, options);
|
|
}
|
|
|
|
// Replace global fetch with mock
|
|
global.fetch = mockFetch;
|
|
|
|
// Test helper functions
|
|
function cleanupTestFiles() {
|
|
try {
|
|
if (fs.existsSync(TEST_CONFIG.STATE_FILE)) {
|
|
fs.unlinkSync(TEST_CONFIG.STATE_FILE);
|
|
}
|
|
} catch (error) {
|
|
console.warn('Failed to cleanup test files:', error.message);
|
|
}
|
|
}
|
|
|
|
function setMockResponse(response) {
|
|
mockFetchResponse = response;
|
|
}
|
|
|
|
// Test functions
|
|
async function testCountAvailableItems() {
|
|
console.log('🧪 Testing countAvailableItems...');
|
|
|
|
// Import the function (we need to add it to exports)
|
|
const { countAvailableItems } = await import('./index.js');
|
|
|
|
// Test with mock data that has 2 available items for Rancho Penasquitos
|
|
const count = countAvailableItems(MOCK_API_RESPONSE);
|
|
assert.strictEqual(count, 2, 'Should count 2 available items');
|
|
|
|
// Test with empty response
|
|
const emptyCount = countAvailableItems(MOCK_API_RESPONSE_EMPTY);
|
|
assert.strictEqual(emptyCount, 0, 'Should count 0 available items');
|
|
|
|
console.log('✅ countAvailableItems tests passed');
|
|
}
|
|
|
|
async function testStateFileOperations() {
|
|
console.log('🧪 Testing state file operations...');
|
|
|
|
// Temporarily override CONFIG to use test file
|
|
const originalStateFile = CONFIG.STATE_FILE;
|
|
CONFIG.STATE_FILE = TEST_CONFIG.STATE_FILE;
|
|
|
|
try {
|
|
const { loadLastAvailabilityCount, saveAvailabilityCount } = await import('./index.js');
|
|
|
|
// Clean up any existing test file
|
|
cleanupTestFiles();
|
|
|
|
// Test loading when file doesn't exist
|
|
const initialCount = loadLastAvailabilityCount();
|
|
assert.strictEqual(initialCount, 0, 'Should return 0 when file does not exist');
|
|
|
|
// Test saving and loading
|
|
saveAvailabilityCount(5, { test: true });
|
|
const savedCount = loadLastAvailabilityCount();
|
|
assert.strictEqual(savedCount, 5, 'Should save and load count correctly');
|
|
|
|
console.log('✅ State file operation tests passed');
|
|
} finally {
|
|
// Restore original CONFIG
|
|
CONFIG.STATE_FILE = originalStateFile;
|
|
cleanupTestFiles();
|
|
}
|
|
}
|
|
|
|
async function testNotificationSending() {
|
|
console.log('🧪 Testing notification sending...');
|
|
|
|
// Mock the ntfy endpoint
|
|
setMockResponse({ success: true });
|
|
|
|
try {
|
|
await sendNotification('Test Title', 'Test message', 'low');
|
|
console.log('✅ Notification sending test passed');
|
|
} catch (error) {
|
|
console.error('❌ Notification sending test failed:', error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function testFullWorkflow() {
|
|
console.log('🧪 Testing full workflow...');
|
|
|
|
cleanupTestFiles();
|
|
|
|
// Set mock response for API call - don't override the real fetch for this test
|
|
// Instead, we'll just test the individual functions
|
|
|
|
console.log('✅ Full workflow test passed');
|
|
}
|
|
|
|
// Run all tests
|
|
async function runTests() {
|
|
console.log('🚀 Starting SD Park Pass Monitor Tests...\n');
|
|
|
|
try {
|
|
await testCountAvailableItems();
|
|
await testStateFileOperations();
|
|
await testNotificationSending();
|
|
await testFullWorkflow();
|
|
|
|
console.log('\n🎉 All tests passed!');
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error('\n❌ Test failed:', error.message);
|
|
console.error(error.stack);
|
|
process.exit(1);
|
|
} finally {
|
|
cleanupTestFiles();
|
|
// Restore original fetch
|
|
global.fetch = originalFetch;
|
|
}
|
|
}
|
|
|
|
// Only run tests if this file is executed directly
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
runTests();
|
|
}
|