/**
* Tests for Luxon integration in filename parsing
* Tests timezone handling with fixed offset strategy
*/
import { describe, it, expect } from 'vitest';
import { parseClusterFilenames } from '@/utils/filenameParser';
describe('Filename Parser Luxon Integration', () => {
describe('parseClusterFilenames with timezone handling', () => {
it('should handle UTC timezone correctly', () => {
const filenames = ["201012_123456.wav", "201014_123456.WAV"];
const result = parseClusterFilenames(filenames, "UTC");
expect(result).toHaveLength(2);
expect(result[0].fileName).toBe("201012_123456.wav");
expect(result[0].timestamp.timestampLocal).toMatch(/^2020-10-12T12:34:56\.000\+00:00$/);
expect(result[0].timestamp.timestampUTC).toMatch(/^2020-10-12T12:34:56\.000Z$/);
});
it('should use fixed offset for entire cluster', () => {
// Test files that span a DST transition period
// In Auckland, DST typically ends in early April (UTC+13 -> UTC+12)
const filenames = [
"200315_120000.wav", // March 15th (summer time, UTC+13)
"200415_120000.wav" // April 15th (would be winter time, UTC+12 if DST applied)
];
const result = parseClusterFilenames(filenames, "Pacific/Auckland");
expect(result).toHaveLength(2);
// Both files should use the same offset (from the first file)
const firstOffset = extractOffsetFromTimestamp(result[0].timestamp.timestampLocal);
const secondOffset = extractOffsetFromTimestamp(result[1].timestamp.timestampLocal);
expect(firstOffset).toBe(secondOffset);
// The offset should be UTC+13 (summer time from March 15th)
expect(firstOffset).toBe("+13:00");
});
it('should calculate correct offset for different timezones', () => {
const filenames = ["20240615_120000.wav"]; // June 15th, 2024
// Test Auckland (should be UTC+12 in winter)
const aucklandResult = parseClusterFilenames(filenames, "Pacific/Auckland");
const aucklandOffset = extractOffsetFromTimestamp(aucklandResult[0].timestamp.timestampLocal);
expect(aucklandOffset).toBe("+12:00");
// Test New York (should be UTC-4 in summer)
const nyResult = parseClusterFilenames(filenames, "America/New_York");
const nyOffset = extractOffsetFromTimestamp(nyResult[0].timestamp.timestampLocal);
expect(nyOffset).toBe("-04:00");
});
it('should handle invalid timezone gracefully', () => {
const filenames = ["20240615_120000.wav"]; // Use 8-digit format to avoid needing multiple files
// Should fallback to UTC format on invalid timezone
expect(() => {
parseClusterFilenames(filenames, "Invalid/Timezone");
}).not.toThrow();
});
it('should sort files chronologically to find first file', () => {
// Files not in chronological order
const filenames = [
"200315_120000.wav", // Later file
"200101_120000.wav", // Earlier file (should be used for offset)
"200215_120000.wav" // Middle file
];
const result = parseClusterFilenames(filenames, "Pacific/Auckland");
expect(result).toHaveLength(3);
// All files should use the same offset (based on earliest file: Jan 1st)
const offsets = result.map(r => extractOffsetFromTimestamp(r.timestamp.timestampLocal));
const uniqueOffsets = new Set(offsets);
expect(uniqueOffsets.size).toBe(1); // All files should have same offset
// The offset should be based on January 1st (UTC+13 in Auckland summer)
expect(offsets[0]).toBe("+13:00");
});
it('should maintain original filename order in results', () => {
const filenames = [
"200315_120000.wav",
"200101_120000.wav",
"200215_120000.wav"
];
const result = parseClusterFilenames(filenames, "UTC");
// Results should maintain original order
expect(result[0].fileName).toBe("200315_120000.wav");
expect(result[1].fileName).toBe("200101_120000.wav");
expect(result[2].fileName).toBe("200215_120000.wav");
});
});
});
/**
* Helper function to extract timezone offset from ISO timestamp
*/
function extractOffsetFromTimestamp(timestamp: string): string {
const match = timestamp.match(/([+-]\d{2}:\d{2})$/);
return match ? match[1] : '';
}