feat: add Vitest unit testing infrastructure with initial test suite.

This commit is contained in:
Xiaohan-Tian
2025-09-03 22:15:05 -07:00
parent be39392aee
commit aa2b19966b
11 changed files with 2673 additions and 13 deletions
@@ -0,0 +1,197 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { CreateNoteCommand } from './CreateNoteCommand'
import { KGCore } from '../../KGCore'
import { KGMidiNote } from '../../midi/KGMidiNote'
import { createMockProject, createMockMidiTrack, createMockMidiRegion } from '../../../test/utils/mock-data'
// Mock the KGCore singleton
vi.mock('../../KGCore', () => ({
KGCore: {
instance: vi.fn()
}
}))
// Mock generateUniqueId utility
vi.mock('../../../util/miscUtil', () => ({
generateUniqueId: vi.fn().mockReturnValue('mock-note-id')
}))
// Import the mocked function properly
const { generateUniqueId } = await import('../../../util/miscUtil')
interface MockCore {
getCurrentProject: ReturnType<typeof vi.fn>
}
describe('CreateNoteCommand', () => {
let mockCore: MockCore
let mockProject: ReturnType<typeof createMockProject>
let mockTrack: ReturnType<typeof createMockMidiTrack>
let mockRegion: ReturnType<typeof createMockMidiRegion>
let command: CreateNoteCommand
beforeEach(() => {
// Create test data
mockRegion = createMockMidiRegion({
id: 'test-region',
trackId: 'test-track',
name: 'Test Region'
})
mockTrack = createMockMidiTrack({
id: 1,
name: 'Test Track',
regions: [mockRegion]
})
mockProject = createMockProject({
name: 'Test Project',
tracks: [mockTrack]
})
// Mock KGCore methods
mockCore = {
getCurrentProject: vi.fn().mockReturnValue(mockProject)
}
vi.mocked(KGCore.instance).mockReturnValue(mockCore as unknown as KGCore)
// Create command
command = new CreateNoteCommand(
'test-region', // regionId
0, // startBeat
1, // endBeat
60, // pitch (middle C)
80 // velocity
)
})
describe('constructor', () => {
it('should create command with correct parameters', () => {
const cmd = new CreateNoteCommand('region-1', 2, 4, 72, 100, 'custom-id')
expect(cmd).toBeInstanceOf(CreateNoteCommand)
// We can't directly test private properties, but we can test execution
})
it('should generate unique ID when not provided', () => {
new CreateNoteCommand('region-1', 0, 1, 60, 80)
// The generateUniqueId mock should have been called
expect(generateUniqueId).toHaveBeenCalledWith('KGMidiNote')
})
it('should use provided ID when given', () => {
// Clear previous calls
vi.clearAllMocks()
new CreateNoteCommand('region-1', 0, 1, 60, 80, 'my-custom-id')
// Should not call generateUniqueId when ID is provided
expect(generateUniqueId).not.toHaveBeenCalled()
})
})
describe('execute', () => {
it('should create a note in the target region', () => {
// Mock the addNote method
const addNoteSpy = vi.spyOn(mockRegion, 'addNote')
// Execute the command
command.execute()
// Verify note was added
expect(addNoteSpy).toHaveBeenCalledTimes(1)
// Verify the note has correct properties
const addedNote = addNoteSpy.mock.calls[0][0] as KGMidiNote
expect(addedNote).toBeInstanceOf(KGMidiNote)
expect(addedNote.getStartBeat()).toBe(0)
expect(addedNote.getEndBeat()).toBe(1)
expect(addedNote.getPitch()).toBe(60)
expect(addedNote.getVelocity()).toBe(80)
})
it('should throw error for non-existent region', () => {
// Create command for non-existent region
const badCommand = new CreateNoteCommand('non-existent-region', 0, 1, 60, 80)
// Should throw error for non-existent region
expect(() => badCommand.execute()).toThrow('MIDI region with ID non-existent-region not found')
})
it('should store created note for undo operation', () => {
const addNoteSpy = vi.spyOn(mockRegion, 'addNote')
command.execute()
// Note should be created and stored internally for undo
expect(addNoteSpy).toHaveBeenCalledTimes(1)
})
})
describe('undo', () => {
it('should remove the created note', () => {
// Execute first to create the note
command.execute()
// Mock removeNote method
const removeNoteSpy = vi.spyOn(mockRegion, 'removeNote')
// Undo the command
command.undo()
// Verify note was removed
expect(removeNoteSpy).toHaveBeenCalledTimes(1)
})
it('should throw error when undoing without execute', () => {
// Try to undo without executing first
expect(() => command.undo()).toThrow('Cannot undo: no note was created')
})
})
describe('re-execute (redo pattern)', () => {
it('should re-add the note after undo using execute', () => {
// Execute, undo, then execute again (redo pattern)
command.execute()
command.undo()
const addNoteSpy = vi.spyOn(mockRegion, 'addNote')
command.execute() // Commands are re-executed for redo
// Note should be added again
expect(addNoteSpy).toHaveBeenCalledTimes(1)
})
})
describe('getDescription', () => {
it('should return descriptive text', () => {
const description = command.getDescription()
expect(description).toBeDefined()
expect(typeof description).toBe('string')
expect(description.length).toBeGreaterThan(0)
})
})
describe('command lifecycle', () => {
it('should support multiple execute/undo cycles', () => {
const addNoteSpy = vi.spyOn(mockRegion, 'addNote')
const removeNoteSpy = vi.spyOn(mockRegion, 'removeNote')
// Execute -> Undo -> Execute -> Undo
command.execute()
expect(addNoteSpy).toHaveBeenCalledTimes(1)
command.undo()
expect(removeNoteSpy).toHaveBeenCalledTimes(1)
command.execute() // Re-execute for redo
expect(addNoteSpy).toHaveBeenCalledTimes(2)
command.undo()
expect(removeNoteSpy).toHaveBeenCalledTimes(2)
})
})
})
+151
View File
@@ -0,0 +1,151 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { KGMidiNote } from './KGMidiNote'
describe('KGMidiNote', () => {
let note: KGMidiNote
beforeEach(() => {
note = new KGMidiNote('test-note', 0, 1, 60, 80)
})
describe('constructor', () => {
it('should create a note with correct properties', () => {
const testNote = new KGMidiNote('note-1', 2, 4, 72, 100)
expect(testNote.getId()).toBe('note-1')
expect(testNote.getStartBeat()).toBe(2)
expect(testNote.getEndBeat()).toBe(4)
expect(testNote.getPitch()).toBe(72)
expect(testNote.getVelocity()).toBe(100)
})
it('should use default values when not provided', () => {
const defaultNote = new KGMidiNote('default-note')
expect(defaultNote.getId()).toBe('default-note')
expect(defaultNote.getStartBeat()).toBe(0)
expect(defaultNote.getEndBeat()).toBe(0)
expect(defaultNote.getPitch()).toBe(0)
expect(defaultNote.getVelocity()).toBe(127)
})
})
describe('getters and setters', () => {
it('should get and set start beat', () => {
expect(note.getStartBeat()).toBe(0)
note.setStartBeat(1.5)
expect(note.getStartBeat()).toBe(1.5)
})
it('should get and set end beat', () => {
expect(note.getEndBeat()).toBe(1)
note.setEndBeat(3.5)
expect(note.getEndBeat()).toBe(3.5)
})
it('should get and set pitch', () => {
expect(note.getPitch()).toBe(60)
note.setPitch(72)
expect(note.getPitch()).toBe(72)
})
it('should get and set velocity', () => {
expect(note.getVelocity()).toBe(80)
note.setVelocity(100)
expect(note.getVelocity()).toBe(100)
})
it('should get and set ID', () => {
expect(note.getId()).toBe('test-note')
note.setId('new-id')
expect(note.getId()).toBe('new-id')
})
})
describe('selection', () => {
it('should start unselected', () => {
expect(note.isSelected()).toBe(false)
})
it('should select and deselect', () => {
note.select()
expect(note.isSelected()).toBe(true)
note.deselect()
expect(note.isSelected()).toBe(false)
})
})
describe('note duration', () => {
it('should calculate duration correctly', () => {
const durationNote = new KGMidiNote('duration-test', 1, 3, 60, 80)
expect(durationNote.getEndBeat() - durationNote.getStartBeat()).toBe(2)
})
it('should handle zero duration', () => {
const zeroDurationNote = new KGMidiNote('zero-duration', 2, 2, 60, 80)
expect(zeroDurationNote.getEndBeat() - zeroDurationNote.getStartBeat()).toBe(0)
})
})
describe('pitch validation', () => {
it('should accept valid MIDI pitch range', () => {
// MIDI pitch range is typically 0-127
note.setPitch(0)
expect(note.getPitch()).toBe(0)
note.setPitch(127)
expect(note.getPitch()).toBe(127)
note.setPitch(60) // Middle C
expect(note.getPitch()).toBe(60)
})
})
describe('velocity validation', () => {
it('should accept valid MIDI velocity range', () => {
// MIDI velocity range is typically 0-127
note.setVelocity(0)
expect(note.getVelocity()).toBe(0)
note.setVelocity(127)
expect(note.getVelocity()).toBe(127)
note.setVelocity(64) // Mid velocity
expect(note.getVelocity()).toBe(64)
})
})
describe('clone and comparison', () => {
it('should create independent instances', () => {
const note1 = new KGMidiNote('note-1', 0, 1, 60, 80)
const note2 = new KGMidiNote('note-2', 0, 1, 60, 80)
expect(note1.getId()).not.toBe(note2.getId())
note1.setPitch(72)
expect(note2.getPitch()).toBe(60) // Should remain unchanged
})
})
describe('edge cases', () => {
it('should handle negative start beat', () => {
note.setStartBeat(-1)
expect(note.getStartBeat()).toBe(-1)
})
it('should handle start beat after end beat', () => {
note.setStartBeat(5)
note.setEndBeat(2)
expect(note.getStartBeat()).toBe(5)
expect(note.getEndBeat()).toBe(2)
// Note: The class might need validation logic to prevent this
})
})
})