fakeapi

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 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.

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 the Processed helper reader (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) CheckAndMark added in v1.4.0

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

CheckAndMark atomically tests and sets the recent-processed window for key, mirroring internal/cache.Cache.CheckAndMark: it returns true and records the current time when key is outside the recent window, or false (leaving the existing timestamp intact) when it is inside. The check and the mark happen under a single lock acquisition so two concurrent callers cannot both observe "not processed" for the same key.

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) 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) 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

DisableSubtitles records the call and returns DisableErr.

func (*Plex) Episode

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

Episode returns the episode identified by key, or plex.ErrNotFound if EpisodeByKey has no matching entry.

func (*Plex) History

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

History returns the HistoryItems slice configured on the fake.

func (*Plex) RecentlyAdded

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

RecentlyAdded returns the recently-added episodes stored under sectionKey in RecentlyAddedBySec.

func (*Plex) SeasonEpisodes

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

SeasonEpisodes returns all episodes stored under key in SeasonEpisodesByKey.

func (*Plex) SetAudioStream

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

SetAudioStream records the call and returns SetAudioErr.

func (*Plex) SetSubtitleStream

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

SetSubtitleStream records the call and returns SetSubtitleErr.

func (*Plex) ShowEpisodes

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

ShowEpisodes returns all episodes stored under showRatingKey in ShowEpisodesByShow.

func (*Plex) ShowMetadata

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

ShowMetadata returns the show metadata stored under key in ShowMetadataByKey, or plex.ErrNotFound if no entry exists.

func (*Plex) ShowSections

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

ShowSections returns the Sections slice configured on the fake.

func (*Plex) UserFromSession

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

UserFromSession returns the userID, username, and error configured in UserFromSessionResult.

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

All returns the AllResult slice configured on the fake.

func (*Users) ClientForUser

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

ClientForUser always returns nil; the fake does not manage per-user clients.

func (*Users) Name

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

Name returns the display name for userID from the Names map, falls back to "user-<userID>" when AllResult is non-nil, or empty string otherwise.

Jump to

Keyboard shortcuts

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