package api

import (
	"sync"
	"testing"

	"github.com/Ptt-official-app/go-pttbbs/bbs"
	"github.com/Ptt-official-app/go-pttbbs/types"
)

func TestWriteFavorites(t *testing.T) {
	setupTest(t.Name())
	defer teardownTest(t.Name())

	content0 := []byte{
		0x23, 0x0d, 0x03, 0x00, 0x02, 0x01, 0x01, 0x01,
		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x01, 0x02,
		0x01, 0x01, 0xb7, 0x73, 0xaa, 0xba, 0xa5, 0xd8,
		0xbf, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x03, 0x01, 0x02, 0x01, 0x01,
		0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x08, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01,
		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00,
	}

	params0 := &WriteFavoritesParams{
		Content: content0,
	}

	path0 := &WriteFavoritesPath{
		UserID: "SYSOP",
	}

	type args struct {
		remoteAddr string
		uuserID    bbs.UUserID
		params     interface{}
		path       interface{}
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		// TODO: Add test cases.
		{
			args: args{remoteAddr: "127.0.0.1", uuserID: "SYSOP", params: params0, path: path0},
		},
	}
	var wg sync.WaitGroup
	for _, tt := range tests {
		wg.Add(1)
		t.Run(tt.name, func(t *testing.T) {
			defer wg.Done()
			now := types.NowTS()
			gotResult, err := WriteFavorites(tt.args.remoteAddr, tt.args.uuserID, tt.args.params, tt.args.path, nil)
			if (err != nil) != tt.wantErr {
				t.Errorf("WriteFavorites() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			ret, _ := gotResult.(*WriteFavoritesResult)
			if ret.MTime-now > 2 {
				t.Errorf("ret.MTime: %v now: %v", ret.MTime, now)
			}
		})
		wg.Wait()
	}
}
