package syncengine

import (
	"testing"
)

func TestQueue(t *testing.T) {
	q := NewQueue()

	if q.IsRunning(1) {
		t.Error("queue should be empty")
	}

	cancelCalled := false
	cancel := func() { cancelCalled = true }

	err := q.Enqueue(1, 100, cancel)
	if err != nil {
		t.Errorf("Enqueue(1) unexpected error: %v", err)
	}

	if !q.IsRunning(1) {
		t.Error("queue should contain syncPair 1")
	}

	jobID, ok := q.GetJobID(1)
	if !ok || jobID != 100 {
		t.Errorf("GetJobID(1) = %d, %v, want 100, true", jobID, ok)
	}

	err = q.Enqueue(1, 200, nil)
	if err != ErrAlreadyRunning {
		t.Errorf("Enqueue(1) again = %v, want ErrAlreadyRunning", err)
	}

	q.Cancel(1, true)
	if !cancelCalled {
		t.Error("Cancel should have called the cancel func")
	}
	if !q.IsCancelledByUser(1) {
		t.Error("IsCancelledByUser should return true after Cancel(1, true)")
	}

	q.Dequeue(1)

	q.Dequeue(1)
	if q.IsRunning(1) {
		t.Error("queue should be empty after Dequeue")
	}
}
