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
149
150
151
152
153
154
155
156
157
|
using System;
using System.Linq;
using Emby.Server.Implementations.Data;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Server.Implementations.Item;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
using Xunit;
using BaseItemKind = Jellyfin.Data.Enums.BaseItemKind;
namespace Jellyfin.Server.Implementations.Tests.Item;
/// <summary>
/// Artists are by-name items: they live outside any library and carry no TopParentId, so a plain
/// item query for them is exempt from the library filter and spans every music library. Only the
/// by-name listings, which reach the artist through the tracks that credit it, can be scoped to
/// one library.
/// </summary>
public sealed class BaseItemRepositoryArtistLibraryScopeTests : SqliteDbTestFixture
{
private static readonly Guid _firstLibrary = Guid.Parse("11111111-0000-0000-0000-000000000001");
private static readonly Guid _secondLibrary = Guid.Parse("22222222-0000-0000-0000-000000000001");
private readonly BaseItemRepository _repository;
private readonly ItemTypeLookup _itemTypeLookup;
public BaseItemRepositoryArtistLibraryScopeTests()
{
_itemTypeLookup = new ItemTypeLookup();
_repository = CreateBaseItemRepository(_itemTypeLookup);
Seed("First Artist", "first artist", _firstLibrary, hasImage: true);
Seed("Second Artist", "second artist", _secondLibrary, hasImage: true);
}
[Fact]
public void GetItemList_MusicArtistsScopedToOneLibrary_ReturnsEveryLibrarysArtists()
{
// The shape the library cover image used to be built from. By-name types are exempt from
// the TopParentId filter, so the scope is silently dropped.
var result = _repository.GetItemList(new InternalItemsQuery
{
DtoOptions = new DtoOptions(false),
IncludeItemTypes = [BaseItemKind.MusicArtist],
TopParentIds = [_firstLibrary]
});
Assert.Equal(["First Artist", "Second Artist"], result.Select(i => i.Name).OrderBy(n => n));
}
[Fact]
public void GetAllArtists_ScopedToOneLibrary_ReturnsOnlyThatLibrarysArtists()
{
var result = _repository.GetAllArtists(new InternalItemsQuery
{
DtoOptions = new DtoOptions(false),
TopParentIds = [_firstLibrary]
});
var (artist, _) = Assert.Single(result.Items);
Assert.Equal("First Artist", artist.Name);
}
[Fact]
public void GetAllArtists_ImageTypes_DropsArtistsWithoutThatImage()
{
// The collage has nothing to draw with an artist that has no image, so the listing has to
// honour the image filter the caller asked for.
Seed("Third Artist", "third artist", _firstLibrary, hasImage: false);
var result = _repository.GetAllArtists(new InternalItemsQuery
{
DtoOptions = new DtoOptions(false),
ImageTypes = [ImageType.Primary],
TopParentIds = [_firstLibrary]
});
var (artist, _) = Assert.Single(result.Items);
Assert.Equal("First Artist", artist.Name);
}
/// <summary>
/// Seeds one by-name artist row and a track in the given library crediting it.
/// </summary>
/// <param name="name">The artist name.</param>
/// <param name="cleanName">The cleaned artist name, which is what links the two rows.</param>
/// <param name="topParentId">The library the track belongs to.</param>
/// <param name="hasImage">Whether the artist row carries a primary image.</param>
private void Seed(string name, string cleanName, Guid topParentId, bool hasImage)
{
using var ctx = CreateDbContext();
var artistId = Guid.NewGuid();
var artist = new BaseItemEntity
{
Id = artistId,
Type = _itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist],
Name = name,
CleanName = cleanName,
PresentationUniqueKey = artistId.ToString("N"),
IsFolder = true,
IsVirtualItem = false
};
if (hasImage)
{
artist.Images =
[
new BaseItemImageInfo
{
Id = Guid.NewGuid(),
ItemId = artistId,
Item = artist,
ImageType = ImageInfoImageType.Primary,
Path = $"/metadata/artists/{cleanName}/folder.jpg"
}
];
}
ctx.BaseItems.Add(artist);
var trackId = Guid.NewGuid();
var track = new BaseItemEntity
{
Id = trackId,
Type = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Audio],
Name = $"{name} - Track",
CleanName = $"{cleanName} - track",
PresentationUniqueKey = trackId.ToString("N"),
MediaType = "Audio",
TopParentId = topParentId,
IsFolder = false,
IsVirtualItem = false
};
ctx.BaseItems.Add(track);
var itemValue = new ItemValue
{
ItemValueId = Guid.NewGuid(),
Type = ItemValueType.AlbumArtist,
Value = name,
CleanValue = cleanName
};
ctx.ItemValues.Add(itemValue);
ctx.ItemValuesMap.Add(new ItemValueMap
{
ItemId = trackId,
ItemValueId = itemValue.ItemValueId,
Item = track,
ItemValue = itemValue
});
ctx.SaveChanges();
}
}
|