refactor: inline single-use helpers in jikan relations
This commit is contained in:
@@ -54,32 +54,8 @@ func watchOrderTypeLabel(value string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isTVWatchOrderType(value string) bool {
|
|
||||||
return strings.EqualFold(strings.TrimSpace(value), "tv")
|
|
||||||
}
|
|
||||||
|
|
||||||
// isAllowedWatchOrderType returns true for the default uncluttered watch order types.
|
|
||||||
func isAllowedWatchOrderType(value string) bool {
|
|
||||||
normalized := strings.ToLower(strings.TrimSpace(value))
|
|
||||||
return normalized == "tv" || normalized == "movie"
|
|
||||||
}
|
|
||||||
|
|
||||||
func hasTVWatchOrderEntry(entries []watchorder.WatchOrderEntry) bool {
|
|
||||||
for _, entry := range entries {
|
|
||||||
if isTVWatchOrderType(entry.Type) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func relationCacheKey(id int) string {
|
|
||||||
return fmt.Sprintf("relations:watch-order:%d", id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) refreshWatchOrder(ctx context.Context, id int) (watchorder.WatchOrderResult, error) {
|
func (c *Client) refreshWatchOrder(ctx context.Context, id int) (watchorder.WatchOrderResult, error) {
|
||||||
cacheKey := relationCacheKey(id)
|
cacheKey := fmt.Sprintf("relations:watch-order:%d", id)
|
||||||
watchOrderURL := fmt.Sprintf(chiakiWatchOrderURL, id)
|
watchOrderURL := fmt.Sprintf(chiakiWatchOrderURL, id)
|
||||||
requestCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
requestCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -153,7 +129,7 @@ func (c *Client) refreshWatchOrderAsync(id int) {
|
|||||||
|
|
||||||
// getWatchOrder fetches watch order from chiaki, caches result for 24h.
|
// getWatchOrder fetches watch order from chiaki, caches result for 24h.
|
||||||
func (c *Client) getWatchOrder(ctx context.Context, id int) (watchorder.WatchOrderResult, error) {
|
func (c *Client) getWatchOrder(ctx context.Context, id int) (watchorder.WatchOrderResult, error) {
|
||||||
cacheKey := relationCacheKey(id)
|
cacheKey := fmt.Sprintf("relations:watch-order:%d", id)
|
||||||
|
|
||||||
var cached watchorder.WatchOrderResult
|
var cached watchorder.WatchOrderResult
|
||||||
if c.getCache(ctx, cacheKey, &cached) {
|
if c.getCache(ctx, cacheKey, &cached) {
|
||||||
@@ -212,7 +188,14 @@ func (c *Client) handleWatchOrderError(ctx context.Context, id int, err error) (
|
|||||||
func buildAllowedWatchOrderEntries(result watchorder.WatchOrderResult, mode WatchOrderMode) ([]watchorder.WatchOrderEntry, map[int]bool) {
|
func buildAllowedWatchOrderEntries(result watchorder.WatchOrderResult, mode WatchOrderMode) ([]watchorder.WatchOrderEntry, map[int]bool) {
|
||||||
allowedEntries := make([]watchorder.WatchOrderEntry, 0, len(result.WatchOrder))
|
allowedEntries := make([]watchorder.WatchOrderEntry, 0, len(result.WatchOrder))
|
||||||
seen := make(map[int]bool)
|
seen := make(map[int]bool)
|
||||||
shouldIncludeAllTypes := mode == WatchOrderModeComplete || !hasTVWatchOrderEntry(result.WatchOrder)
|
hasTVEntry := false
|
||||||
|
for _, entry := range result.WatchOrder {
|
||||||
|
if strings.EqualFold(strings.TrimSpace(entry.Type), "tv") {
|
||||||
|
hasTVEntry = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
shouldIncludeAllTypes := mode == WatchOrderModeComplete || !hasTVEntry
|
||||||
|
|
||||||
for _, entry := range result.WatchOrder {
|
for _, entry := range result.WatchOrder {
|
||||||
if len(allowedEntries) >= maxWatchOrderEntries {
|
if len(allowedEntries) >= maxWatchOrderEntries {
|
||||||
@@ -221,7 +204,8 @@ func buildAllowedWatchOrderEntries(result watchorder.WatchOrderResult, mode Watc
|
|||||||
if seen[entry.ID] {
|
if seen[entry.ID] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !shouldIncludeAllTypes && !isAllowedWatchOrderType(entry.Type) {
|
normalizedType := strings.ToLower(strings.TrimSpace(entry.Type))
|
||||||
|
if !shouldIncludeAllTypes && normalizedType != "tv" && normalizedType != "movie" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,40 +5,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runBoolCases(t *testing.T, tests []struct {
|
|
||||||
name string
|
|
||||||
input string
|
|
||||||
want bool
|
|
||||||
}, fn func(string) bool) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
for _, testCase := range tests {
|
|
||||||
t.Run(testCase.name, func(t *testing.T) {
|
|
||||||
got := fn(testCase.input)
|
|
||||||
if got != testCase.want {
|
|
||||||
t.Fatalf("expected %v, got %v", testCase.want, got)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsAllowedWatchOrderType(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
input string
|
|
||||||
want bool
|
|
||||||
}{
|
|
||||||
{name: "tv", input: "tv", want: true},
|
|
||||||
{name: "movie", input: "movie", want: true},
|
|
||||||
{name: "case and whitespace", input: " TV ", want: true},
|
|
||||||
{name: "tv special", input: "tv special", want: false},
|
|
||||||
{name: "ova", input: "ova", want: false},
|
|
||||||
{name: "empty", input: "", want: false},
|
|
||||||
}
|
|
||||||
|
|
||||||
runBoolCases(t, tests, isAllowedWatchOrderType)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNormalizeWatchOrderMode(t *testing.T) {
|
func TestNormalizeWatchOrderMode(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -62,46 +28,12 @@ func TestNormalizeWatchOrderMode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHasTVWatchOrderEntry(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
entries []watchorder.WatchOrderEntry
|
|
||||||
want bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "contains tv",
|
|
||||||
entries: []watchorder.WatchOrderEntry{
|
|
||||||
{ID: 1, Type: "Movie"},
|
|
||||||
{ID: 2, Type: " TV "},
|
|
||||||
},
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "ona only",
|
|
||||||
entries: []watchorder.WatchOrderEntry{
|
|
||||||
{ID: 1, Type: "ONA"},
|
|
||||||
{ID: 2, Type: "Special"},
|
|
||||||
},
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, testCase := range tests {
|
|
||||||
t.Run(testCase.name, func(t *testing.T) {
|
|
||||||
got := hasTVWatchOrderEntry(testCase.entries)
|
|
||||||
if got != testCase.want {
|
|
||||||
t.Fatalf("expected %v, got %v", testCase.want, got)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBuildAllowedWatchOrderEntriesKeepsDefaultTypesWhenTVExists(t *testing.T) {
|
func TestBuildAllowedWatchOrderEntriesKeepsDefaultTypesWhenTVExists(t *testing.T) {
|
||||||
result := watchorder.WatchOrderResult{
|
result := watchorder.WatchOrderResult{
|
||||||
WatchOrder: []watchorder.WatchOrderEntry{
|
WatchOrder: []watchorder.WatchOrderEntry{
|
||||||
{ID: 1, Type: "TV"},
|
{ID: 1, Type: "TV"},
|
||||||
{ID: 2, Type: "Special"},
|
{ID: 2, Type: "Special"},
|
||||||
{ID: 3, Type: "Movie"},
|
{ID: 3, Type: " Movie "},
|
||||||
{ID: 4, Type: "ONA"},
|
{ID: 4, Type: "ONA"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -193,17 +125,3 @@ func TestWatchOrderTypeLabel(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAllowedWatchOrderTypeFromDataset(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
input string
|
|
||||||
want bool
|
|
||||||
}{
|
|
||||||
{name: "label tv", input: "TV", want: true},
|
|
||||||
{name: "label movie", input: "Movie", want: true},
|
|
||||||
{name: "label special", input: "Special", want: false},
|
|
||||||
}
|
|
||||||
|
|
||||||
runBoolCases(t, tests, isAllowedWatchOrderType)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user