aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests/Item/AlternateVersionQueryTranslationTests.cs
blob: 823ff566a700a403fa4034556c7046f1667d085e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#pragma warning disable RS0030 // Do not use banned APIs: Guid == is required inside EF expression trees to mirror the production query shapes.

using System;
using System.Linq;
using Jellyfin.Database.Implementations;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Database.Implementations.Locking;
using Jellyfin.Database.Providers.Sqlite;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;

namespace Jellyfin.Server.Implementations.Tests.Item;

/// <summary>
/// Verifies that the alternate-version-aware query shapes used by the resume filter
/// (BaseItemRepository.TranslateQuery) and the DatePlayed ordering (OrderMapper) translate
/// and evaluate correctly on the SQLite provider.
/// </summary>
public sealed class AlternateVersionQueryTranslationTests : IDisposable
{
    private readonly SqliteConnection _connection;
    private readonly DbContextOptions<JellyfinDbContext> _dbOptions;

    public AlternateVersionQueryTranslationTests()
    {
        _connection = new SqliteConnection("Data Source=:memory:");
        _connection.Open();

        _dbOptions = new DbContextOptionsBuilder<JellyfinDbContext>()
            .UseSqlite(_connection)
            .Options;

        using var ctx = CreateDbContext();
        ctx.Database.EnsureCreated();
    }

    [Fact]
    public void ResumeFilter_VersionProgress_SurfacesPrimary()
    {
        Guid userId, primaryId, otherId;

        using (var ctx = CreateDbContext())
        {
            (userId, primaryId, otherId) = Seed(ctx);
        }

        using (var ctx = CreateDbContext())
        {
            // Mirrors the resumable filter in BaseItemRepository.TranslateQuery: progress on any
            // version coalesces onto the primary's id.
            var inProgress = ctx.UserData
                .Where(ud => ud.UserId == userId && ud.PlaybackPositionTicks > 0);
            var resumableMovieIds = inProgress
                .Join(ctx.BaseItems, ud => ud.ItemId, bi => bi.Id, (ud, bi) => bi.PrimaryVersionId ?? bi.Id);

            // Scope to the seeded items; EnsureCreated also seeds a placeholder row.
            var seededIds = new[] { primaryId, otherId };

            var resumable = ctx.BaseItems
                .Where(e => seededIds.Contains(e.Id) && e.PrimaryVersionId == null)
                .Where(e => resumableMovieIds.Contains(e.Id))
                .Select(e => e.Id)
                .ToList();

            Assert.Equal([primaryId], resumable);

            // The inverse (not-resumable) direction must exclude the primary as well.
            var notResumable = ctx.BaseItems
                .Where(e => seededIds.Contains(e.Id) && e.PrimaryVersionId == null)
                .Where(e => resumableMovieIds.Contains(e.Id) == false)
                .Select(e => e.Id)
                .ToList();

            Assert.Equal([otherId], notResumable);
        }
    }

    [Fact]
    public void DatePlayedOrdering_VersionProgress_SortsPrimaryByVersionDate()
    {
        Guid userId, primaryId, otherId;

        using (var ctx = CreateDbContext())
        {
            (userId, primaryId, otherId) = Seed(ctx);
        }

        using (var ctx = CreateDbContext())
        {
            // Scope to the seeded items; EnsureCreated also seeds a placeholder row.
            var seededIds = new[] { primaryId, otherId };

            // Mirrors the DatePlayed mapping in OrderMapper.
            var ordered = ctx.BaseItems
                .Where(e => seededIds.Contains(e.Id) && e.PrimaryVersionId == null)
                .OrderByDescending(e => ctx.UserData
                    .Where(w => w.UserId == userId && (w.ItemId == e.Id || w.Item!.PrimaryVersionId == e.Id))
                    .Max(f => f.LastPlayedDate))
                .Select(e => e.Id)
                .ToList();

            // The movie whose only progress is on its alternate version sorts before the unplayed one.
            Assert.Equal([primaryId, otherId], ordered);
        }
    }

    private static (Guid UserId, Guid PrimaryId, Guid OtherId) Seed(JellyfinDbContext ctx)
    {
        var user = new User("test", "auth-provider", "reset-provider");
        ctx.Users.Add(user);

        var primary = new BaseItemEntity { Id = Guid.NewGuid(), Type = "MediaBrowser.Controller.Entities.Movies.Movie" };
        var version = new BaseItemEntity { Id = Guid.NewGuid(), Type = "MediaBrowser.Controller.Entities.Movies.Movie", PrimaryVersionId = primary.Id };
        var other = new BaseItemEntity { Id = Guid.NewGuid(), Type = "MediaBrowser.Controller.Entities.Movies.Movie" };
        ctx.BaseItems.AddRange(primary, version, other);

        // Progress only on the alternate version.
        ctx.UserData.Add(new UserData
        {
            ItemId = version.Id,
            Item = version,
            UserId = user.Id,
            User = user,
            CustomDataKey = version.Id.ToString("N"),
            PlaybackPositionTicks = 1000,
            LastPlayedDate = new DateTime(2026, 1, 2, 0, 0, 0, DateTimeKind.Utc)
        });

        ctx.SaveChanges();
        return (user.Id, primary.Id, other.Id);
    }

    private JellyfinDbContext CreateDbContext()
    {
        return new JellyfinDbContext(
            _dbOptions,
            NullLogger<JellyfinDbContext>.Instance,
            new SqliteDatabaseProvider(null!, NullLogger<SqliteDatabaseProvider>.Instance),
            new NoLockBehavior(NullLogger<NoLockBehavior>.Instance));
    }

    public void Dispose()
    {
        _connection.Dispose();
    }
}