Rules for timing, frame rate, motion blur, deflicker, regions, holy-grail transitions, white balance, stabilisation, aspect, output size and export preset, each with a localisable reason and confidence; Apply fills Auto values without touching manual ones; Optimise takes per-section module decisions and reports changes; content-driven presets round-trip as JSON. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
133 lines
6.4 KiB
C#
133 lines
6.4 KiB
C#
using System.Collections;
|
|
using System.Globalization;
|
|
using System.Reflection;
|
|
using System.Resources;
|
|
using System.Text.RegularExpressions;
|
|
using Titano.Core.Advisor;
|
|
using Titano.Core.Analysis;
|
|
using Titano.Core.Analysis.Deep;
|
|
using Titano.Core.Diagnostics.Synthetic;
|
|
using Titano.Core.Localization;
|
|
using Titano.Core.Project;
|
|
using Titano.Core.Tests.Analysis.Deep;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Core.Tests.Advisor;
|
|
|
|
using AdvisorApi = Titano.Core.Advisor.Advisor;
|
|
|
|
public sealed partial class AdvisorLocalizationTests(TestContext context)
|
|
{
|
|
private static readonly CultureInfo English = CultureInfo.GetCultureInfo("en");
|
|
private static readonly CultureInfo Italian = CultureInfo.GetCultureInfo("it");
|
|
|
|
[Test]
|
|
public void EveryAdvisorKeyHasEnglishAndItalianTextWithTheSamePlaceholders()
|
|
{
|
|
var manager = new ResourceManager("Titano.Core.i18n.Advisor", typeof(Loc).Assembly);
|
|
var english = Entries(manager.GetResourceSet(CultureInfo.InvariantCulture, createIfNotExists: true, tryParents: false)!);
|
|
var italian = Entries(manager.GetResourceSet(Italian, createIfNotExists: true, tryParents: false)!);
|
|
var keys = Keys(typeof(AdvisorKeys)).Concat(Keys(typeof(FindingKeys))).Concat(Keys(typeof(DeepFindingKeys))).ToList();
|
|
Assert.Greater(keys.Count, 90);
|
|
foreach (var key in keys)
|
|
{
|
|
Assert.True(english.TryGetValue(key, out var en), $"English text missing for {key}");
|
|
Assert.True(italian.TryGetValue(key, out var it), $"Italian text missing for {key}");
|
|
Assert.SequenceEqual(Placeholders(en!), Placeholders(it!), key);
|
|
Assert.LessOrEqual(en!.Length, 200);
|
|
}
|
|
Assert.Equal(english.Count, keys.Count);
|
|
}
|
|
|
|
[Test]
|
|
public async Task EveryReasonAndFindingOfTheGoldenScenesFormatsInBothLanguages()
|
|
{
|
|
var scenes = new List<(SyntheticFrameSource Source, DeepAnalysisReport? Deep)>
|
|
{
|
|
(new SyntheticFrameSource(DeepScenes.NightSky()), await DeepReports.Night),
|
|
(new SyntheticFrameSource(DeepScenes.NightSky(jitter: 2), 2), await DeepReports.NightJitter),
|
|
(new SyntheticFrameSource(DeepScenes.HolyGrail()), await DeepReports.HolyGrail),
|
|
(new SyntheticFrameSource(DeepScenes.HolyGrail()) { StripExposure = true }, null),
|
|
};
|
|
var rendered = 0;
|
|
foreach (var (source, deep) in scenes)
|
|
{
|
|
var frames = Enumerable.Range(0, source.Count).Select(source.Metadata).ToList();
|
|
var light = LightAnalyzer.Analyze(frames);
|
|
var sequence = new SequenceContext { FrameCount = frames.Count, SourceWidth = 6000, SourceHeight = 4000, MedianExposureSeconds = 1.0 / 250 };
|
|
var result = AdvisorApi.Recommend(light, deep, sequence);
|
|
foreach (var r in result.Recommendations) rendered += Render(r.ReasonKey, [.. r.ReasonArgs]);
|
|
foreach (var f in result.Findings) rendered += Render(f.Key, f.Arguments);
|
|
foreach (var p in SequencePresets.Generate(light, deep, sequence))
|
|
{
|
|
rendered += Render(p.NameKey, [.. p.NameArgs]);
|
|
rendered += Render(p.ReasonKey!, [.. p.ReasonArgs]);
|
|
}
|
|
// Stored reasons (culture-neutral strings) render with the current culture's numbers.
|
|
var applied = AdvisorApi.Apply(new ProjectSettings(), result);
|
|
Assert.DoesNotContain('⟦', AdvisorApi.Explain(applied.Exposure.HolyGrail.ReasonKey!, applied.Exposure.HolyGrail.ReasonArgs));
|
|
}
|
|
context.Log($"{rendered} texts rendered");
|
|
Assert.Greater(rendered, 80);
|
|
}
|
|
|
|
[Test]
|
|
public void StoredNumericArgumentsAreLocalisedWhenExplained()
|
|
{
|
|
var text = Format(Loc.Get(AdvisorKeys.DeflickerOn, Italian)!, Italian, 0.125);
|
|
Assert.Contains("0,125", text);
|
|
Assert.Contains("0.125", Format(Loc.Get(AdvisorKeys.DeflickerOn, English)!, English, 0.125));
|
|
}
|
|
|
|
[Test]
|
|
public async Task PresetsRoundTripAsJsonAndSaveAsUserPresets()
|
|
{
|
|
var source = new SyntheticFrameSource(DeepScenes.NightSky(jitter: 2), 2);
|
|
var frames = Enumerable.Range(0, source.Count).Select(source.Metadata).ToList();
|
|
var presets = SequencePresets.Generate(LightAnalyzer.Analyze(frames), await DeepReports.NightJitter, new SequenceContext { SourceWidth = 6000, SourceHeight = 4000 });
|
|
foreach (var preset in presets)
|
|
{
|
|
var json = preset.ToJson();
|
|
Assert.Equal(json, SequencePreset.FromJson(json).ToJson());
|
|
}
|
|
var user = presets[0].AsUserPreset(" My night ");
|
|
Assert.True(user.IsUser);
|
|
Assert.Equal("My night", user.Name);
|
|
Assert.NotEqual(presets[0].Id, user.Id);
|
|
var restored = SequencePreset.FromJson(user.ToJson());
|
|
Assert.Equal("My night", restored.DisplayName());
|
|
Assert.Equal(presets[0].Settings.ToJson(), restored.Settings.ToJson());
|
|
Assert.Equal(presets[0].ExportPresetId, restored.ExportPresetId);
|
|
}
|
|
|
|
private static int Render(string key, object?[] args)
|
|
{
|
|
foreach (var culture in new[] { English, Italian })
|
|
{
|
|
var format = Loc.Get(key, culture);
|
|
Assert.NotNull(format);
|
|
var text = Format(format!, culture, args);
|
|
Assert.False(text.Contains('{'), $"{key} left a placeholder: {text}");
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
private static string Format(string format, CultureInfo culture, params object?[] args) => string.Format(culture, format, args);
|
|
|
|
private static Dictionary<string, string> Entries(ResourceSet set)
|
|
{
|
|
var entries = new Dictionary<string, string>(StringComparer.Ordinal);
|
|
foreach (DictionaryEntry entry in set) entries[(string)entry.Key] = (string)entry.Value!;
|
|
return entries;
|
|
}
|
|
|
|
private static IEnumerable<string> Keys(Type type) =>
|
|
type.GetFields(BindingFlags.Public | BindingFlags.Static).Where(f => f.IsLiteral && f.FieldType == typeof(string) && f.Name != nameof(AdvisorKeys.MissingPrefix)).Select(f => (string)f.GetRawConstantValue()!)
|
|
.Concat(type.GetProperties(BindingFlags.Public | BindingFlags.Static).Where(p => p.PropertyType == typeof(string)).Select(p => (string)p.GetValue(null)!));
|
|
|
|
private static IEnumerable<string> Placeholders(string text) => PlaceholderRegex().Matches(text).Select(m => m.Groups[1].Value).Order();
|
|
|
|
[GeneratedRegex(@"\{(\d+)(?:[,:][^}]*)?\}")]
|
|
private static partial Regex PlaceholderRegex();
|
|
}
|