Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions ImageDescriber.Test/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,57 @@
CollectionAssert.AreEquivalent(expectedKeys, Program.Settings.Descriptions.Keys.ToArray());
}

[TestMethod]
public void MergeEntriesStoresAnUpperCaseHashAsTheLowerCaseHashScanLooksUp()
{
Program.Settings = new PersistentState();
string lowerHash = new('a', ImageHasher.HashLength);
string upperHash = lowerHash.ToUpperInvariant();
AbsoluteFilePath firstPath = Path.Combine(Path.GetTempPath(), "first.jpg").As<AbsoluteFilePath>();
AbsoluteFilePath secondPath = Path.Combine(Path.GetTempPath(), "second.jpg").As<AbsoluteFilePath>();

(int NewCount, int UpdatedCount, int SkippedCount) first = default;
(int NewCount, int UpdatedCount, int SkippedCount) second = default;
CaptureConsole(() => first = Import.MergeEntries([new() { Hash = upperHash, KnownPaths = [firstPath] }]));
CaptureConsole(() => second = Import.MergeEntries([new() { Hash = lowerHash, KnownPaths = [secondPath] }]));

Assert.AreEqual(1, first.NewCount);
Assert.AreEqual(1, second.UpdatedCount);
string[] expectedKeys = [lowerHash];
CollectionAssert.AreEquivalent(expectedKeys, Program.Settings.Descriptions.Keys.ToArray());

Check warning on line 75 in ImageDescriber.Test/SearchTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEquivalent'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_ImageDescriber&issues=AaEYfN1Glz4x6_5jz7tM&open=AaEYfN1Glz4x6_5jz7tM&pullRequest=199
Assert.AreEqual(lowerHash, Program.Settings.Descriptions[lowerHash].Hash);
Assert.AreEqual(2, Program.Settings.Descriptions[lowerHash].KnownPaths.Count);

Check warning on line 77 in ImageDescriber.Test/SearchTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.HasCount' instead of 'Assert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_ImageDescriber&issues=AaEYfN1Glz4x6_5jz7tN&open=AaEYfN1Glz4x6_5jz7tN&pullRequest=199
}

[TestMethod]
public void MergeEntriesRepairsNullFieldsSoSearchStatsAndExportStillRun()
{
Program.Settings = new PersistentState();
string hash = new('b', ImageHasher.HashLength);
List<ImageDescription> entries =
[
new()
{
Hash = hash,
Description = null!,
KnownPaths = null!,
SuggestedFileName = null!,
Model = null!,
},
];

CaptureConsole(() => Import.MergeEntries(entries));

ImageDescription stored = Program.Settings.Descriptions[hash];
Assert.IsNotNull(stored.Description);
Assert.IsNotNull(stored.KnownPaths);
Assert.IsNotNull(stored.SuggestedFileName);
Assert.IsNotNull(stored.Model);
CaptureConsole(() => new Search().Run(new Search { Query = "dog" }));
CaptureConsole(() => new Stats().Run(new Stats()));
StringAssert.Contains(Export.BuildCsv(Program.Settings.Descriptions.Values), hash);

Check warning on line 106 in ImageDescriber.Test/SearchTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.Contains' instead of 'StringAssert.Contains'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_ImageDescriber&issues=AaEYfN1Glz4x6_5jz7tO&open=AaEYfN1Glz4x6_5jz7tO&pullRequest=199
}

[TestMethod]
public void IsValidHashAcceptsOnlySha256HexStrings()
{
Expand Down
17 changes: 17 additions & 0 deletions ImageDescriber/Verbs/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ internal static (int NewCount, int UpdatedCount, int SkippedCount) MergeEntries(
continue;
}

// Scan looks hashes up as ComputeHash spells them, lower-case, and the store is keyed
// case-sensitively, so an upper-case hash from another tool would never match.
entry.Hash = entry.Hash.ToLowerInvariant();
RepairNullFields(entry);

if (Program.Settings.Descriptions.TryGetValue(entry.Hash, out ImageDescription? existing))
{
if (MergeKnownPaths(entry, existing))
Expand All @@ -129,6 +134,18 @@ internal static (int NewCount, int UpdatedCount, int SkippedCount) MergeEntries(
return (newCount, updatedCount, skippedCount);
}

/// <summary>
/// Defaults the fields a JSON import can set to null. Search, Stats, Export and Scan all
/// dereference them, so a stored null would make each of them throw on every later run.
/// </summary>
internal static void RepairNullFields(ImageDescription entry)
{
entry.KnownPaths = entry.KnownPaths is null ? [] : [.. entry.KnownPaths.Where(p => p is not null)];
entry.Description ??= string.Empty;
entry.SuggestedFileName ??= string.Empty.As<FileName>();
entry.Model ??= string.Empty.As<OllamaModelName>();
}

internal static bool MergeKnownPaths(ImageDescription source, ImageDescription target)
{
bool pathsAdded = false;
Expand Down
Loading