package smsstore

import (
	"testing"
	"time"
)

func TestIssueAndConsume(t *testing.T) {
	s := New(time.Minute, time.Second, "654321")
	code, exp, retry, err := s.Issue(PurposeLogin, "13800138000")
	if err != nil || code != "654321" || exp <= 0 || retry != 0 {
		t.Fatalf("issue %q %d %d %v", code, exp, retry, err)
	}
	if err := s.Consume(PurposeLogin, "13800138000", "000000"); err == nil {
		t.Fatal("bad code should fail")
	}
	if err := s.Consume(PurposeLogin, "13800138000", "654321"); err != nil {
		t.Fatal(err)
	}
	if err := s.Consume(PurposeLogin, "13800138000", "654321"); err == nil {
		t.Fatal("consumed twice")
	}
}
