fakeapi

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2026 License: GPL-2.0, GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package fakeapi provides shared concurrency-safe test fakes for the interfaces in internal/api. Consumes no I/O. Import from _test.go files only — the package name carries no internal build tag, but callers across internal/{sync,scheduler,notify,users} tests previously declared three near-identical fakeCache types; this package consolidates them into one honest implementation.

Scaffolded in cycle-2 step 1 (arch-fakeapi-p1). Step 3 replaces the three in-package fakeCache copies with fakeapi.NewCache() and deletes the per-test duplicates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunCacheContract

func RunCacheContract(t *testing.T, c api.Cache)

RunCacheContract exercises the api.Cache contract against any implementation. Both cache.Cache and fakeapi.Cache must pass.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a concurrency-safe in-memory implementation of api.Cache. Every accessor takes a short-held lock; consumers can share one Cache across goroutines without additional synchronization.

Beyond api.Cache, Cache exposes Processed / Tokens / SetTokens helper readers (NOT part of api.Cache) that tests use to inspect the fake's state after a run.

func NewCache

func NewCache() *Cache

NewCache returns a ready-to-use zero-seeded Cache with the default recent-window of 5 minutes, matching internal/cache.Cache's WasRecentlyProcessed behavior.

func (*Cache) LastSchedulerRun

func (c *Cache) LastSchedulerRun() time.Time

LastSchedulerRun returns the recorded last-run timestamp. Zero value indicates "never run".

func (*Cache) LearnLanguageProfile

func (c *Cache) LearnLanguageProfile(userID, audioLang, subtitleLang string)

LearnLanguageProfile stores a user's audio→subtitle preference. Empty audioLang is ignored to match internal/cache.Cache.

func (*Cache) MarkProcessed

func (c *Cache) MarkProcessed(key string)

MarkProcessed records the current time against the key.

func (*Cache) Processed

func (c *Cache) Processed() []string

Processed returns a deterministically-ordered copy of the processed keys. Useful for asserting "the sync pass marked exactly these episodes as processed" without relying on Go's map-iteration order.

func (*Cache) SetLastSchedulerRun

func (c *Cache) SetLastSchedulerRun(t time.Time)

SetLastSchedulerRun records the supplied timestamp.

func (*Cache) SetTokens

func (c *Cache) SetTokens(tokens map[string]string)

SetTokens is a shim for SetUserTokens to keep step-3 call-site migration mechanical (the pre-consolidation fakeCache variants use both names).

func (*Cache) SetUserTokens

func (c *Cache) SetUserTokens(tokens map[string]string)

SetUserTokens replaces the token map wholesale. Nil clears the map to an empty non-nil value.

func (*Cache) SubtitleLangForAudio

func (c *Cache) SubtitleLangForAudio(userID, audioLang string) (string, bool)

SubtitleLangForAudio returns the learned subtitle language for the given audio language and user.

func (*Cache) Tokens

func (c *Cache) Tokens() map[string]string

Tokens returns a defensive copy of the internal token map. Identical to UserTokens but named to match the existing per-test helpers that step 3 replaces.

func (*Cache) UserTokens

func (c *Cache) UserTokens() map[string]string

UserTokens returns a defensive copy of the userID → accessToken map.

func (*Cache) WasRecentlyProcessed

func (c *Cache) WasRecentlyProcessed(key string) bool

WasRecentlyProcessed reports whether the key was MarkProcessed'd within the recent window.

type Plex

type Plex struct {
	UserFromSessionResult struct {
		Err      error
		UserID   string
		Username string
	}
	EpisodeErr          error
	SetAudioErr         error
	SetSubtitleErr      error
	DisableErr          error
	ShowEpisodesByShow  map[string][]streams.Episode
	EpisodeByKey        map[string]*streams.Episode
	SeasonEpisodesByKey map[string][]streams.Episode
	ShowMetadataByKey   map[string]*plex.Show
	RecentlyAddedBySec  map[string][]streams.Episode
	HistoryItems        []plex.HistoryItem
	Sections            []plex.Section

	Calls atomic.Int64
	// contains filtered or unexported fields
}

Plex implements api.PlexReadWriter for tests. All methods are concurrency-safe. Fields are exported for direct test setup.

func (*Plex) CallNames

func (f *Plex) CallNames() []string

CallNames returns a copy of the ordered call log.

func (*Plex) DisableSubtitles

func (f *Plex) DisableSubtitles(_ context.Context, _ int) error

func (*Plex) Episode

func (f *Plex) Episode(_ context.Context, key plex.RatingKey) (*streams.Episode, error)

func (*Plex) History

func (f *Plex) History(_ context.Context, _ int64) ([]plex.HistoryItem, error)

func (*Plex) RecentlyAdded

func (f *Plex) RecentlyAdded(_ context.Context, sectionKey plex.RatingKey, _ int64) ([]streams.Episode, error)

func (*Plex) SeasonEpisodes

func (f *Plex) SeasonEpisodes(_ context.Context, key plex.RatingKey) ([]streams.Episode, error)

func (*Plex) SetAudioStream

func (f *Plex) SetAudioStream(_ context.Context, _, _ int) error

func (*Plex) SetSubtitleStream

func (f *Plex) SetSubtitleStream(_ context.Context, _, _ int) error

func (*Plex) ShowEpisodes

func (f *Plex) ShowEpisodes(_ context.Context, showRatingKey plex.RatingKey) ([]streams.Episode, error)

func (*Plex) ShowMetadata

func (f *Plex) ShowMetadata(_ context.Context, key plex.RatingKey) (*plex.Show, error)

func (*Plex) ShowSections

func (f *Plex) ShowSections(_ context.Context) ([]plex.Section, error)

func (*Plex) UserFromSession

func (f *Plex) UserFromSession(_ context.Context, _ string) (userID, username string, err error)

type Users

type Users struct {
	Names     map[string]string
	AllResult []api.UserInfo
}

Users implements api.UserLookup for tests. The zero value returns nil/empty from all methods, matching the minimal scheduler fake.

func (*Users) All

func (u *Users) All() []api.UserInfo

func (*Users) ClientForUser

func (u *Users) ClientForUser(_ string, _ *plex.Client) *plex.Client

func (*Users) Name

func (u *Users) Name(userID string) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL