mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-06-21 17:41:14 +00:00
e9d6db077c
Two integration-test failures uncovered after the initial commit: 1) GetTilesByRegionAsync outer ORDER BY referenced 'updated_at' but the inner DISTINCT ON subquery aliased it to 'UpdatedAt' (Postgres folds to 'updatedat'). DISTINCT ON already guarantees one row per (latitude, longitude, ...) so the third tiebreak was unreachable; removed it. 2) Dapper 2.1.35 silently bypasses SqlMapper.TypeHandler<T> for enum types during read deserialization (Dapper issue #259). The TileSourceTypeHandler worked for writes but reads fell through to Enum.TryParse, which cannot map 'google_maps' to GoogleMaps. Pivoted: TileEntity.Source is now a string (the wire value). TileSource enum stays as the public producer surface in Common.Enums; TileSourceConverter (Common.Enums) provides ToWireValue / FromWireValue / IsValidWireValue at the boundary. TileSourceTypeHandler deleted; registration removed from DapperEnumTypeHandlers.RegisterAll. tile-storage.md Inv-5 amended to document the storage choice. _docs/LESSONS.md L-001 records the Dapper bypass for future cycles. Full suite passes (213 unit + integration suite incl. AZ-484 AC-1..AC-5, security SEC-01..SEC-04, AZ-356/362/357). Co-authored-by: Cursor <cursoragent@cursor.com>
579 lines
23 KiB
C#
579 lines
23 KiB
C#
using FluentAssertions;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using Moq;
|
|
using SatelliteProvider.Common.Configs;
|
|
using SatelliteProvider.Common.DTO;
|
|
using SatelliteProvider.Common.Enums;
|
|
using SatelliteProvider.Common.Interfaces;
|
|
using SatelliteProvider.DataAccess.Models;
|
|
using SatelliteProvider.DataAccess.Repositories;
|
|
using SatelliteProvider.Services.TileDownloader;
|
|
using SatelliteProvider.Tests.Fixtures;
|
|
|
|
namespace SatelliteProvider.Tests;
|
|
|
|
public class TileServiceTests
|
|
{
|
|
private static TileService BuildService(
|
|
Mock<ISatelliteDownloader> downloader,
|
|
Mock<ITileRepository> tileRepo,
|
|
IMemoryCache? cache = null)
|
|
{
|
|
return new TileService(
|
|
downloader.Object,
|
|
tileRepo.Object,
|
|
cache ?? new MemoryCache(new MemoryCacheOptions()),
|
|
Options.Create(new MapConfig { Service = "GoogleMaps", ApiKey = "" }),
|
|
NullLogger<TileService>.Instance);
|
|
}
|
|
|
|
private static DownloadedTileInfoV2 MakeDownloaded(int x, int y, int zoom, double lat, double lon, string filePath = "tiles/18/0/0/tile.jpg")
|
|
{
|
|
return new DownloadedTileInfoV2(x, y, zoom, lat, lon, filePath, 100.0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DownloadAndStoreTilesAsync_NoExistingTiles_StoresAllDownloadedTiles_BT01()
|
|
{
|
|
// Arrange
|
|
var downloader = new Mock<ISatelliteDownloader>();
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
|
|
tileRepo
|
|
.Setup(r => r.GetTilesByRegionAsync(
|
|
TestCoordinates.TileLat, TestCoordinates.TileLon, 200, TestCoordinates.DefaultZoom))
|
|
.ReturnsAsync(Array.Empty<TileEntity>());
|
|
|
|
var downloaded = new List<DownloadedTileInfoV2>
|
|
{
|
|
MakeDownloaded(123, 456, TestCoordinates.DefaultZoom, TestCoordinates.TileLat, TestCoordinates.TileLon),
|
|
};
|
|
downloader
|
|
.Setup(d => d.GetTilesWithMetadataAsync(
|
|
It.IsAny<GeoPoint>(),
|
|
It.IsAny<double>(),
|
|
TestCoordinates.DefaultZoom,
|
|
It.Is<IEnumerable<ExistingTileInfo>>(e => !e.Any()),
|
|
It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(downloaded);
|
|
|
|
var service = BuildService(downloader, tileRepo);
|
|
|
|
// Act
|
|
var result = await service.DownloadAndStoreTilesAsync(
|
|
TestCoordinates.TileLat, TestCoordinates.TileLon, 200, TestCoordinates.DefaultZoom);
|
|
|
|
// Assert
|
|
result.Should().HaveCount(1);
|
|
result[0].TileZoom.Should().Be(TestCoordinates.DefaultZoom);
|
|
result[0].TileSizePixels.Should().Be(256);
|
|
result[0].ImageType.Should().Be("jpg");
|
|
result[0].FilePath.Should().NotBeNullOrEmpty();
|
|
tileRepo.Verify(r => r.InsertAsync(It.IsAny<TileEntity>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DownloadAndStoreTilesAsync_HasCachedTiles_PassesThemToDownloader_BT02()
|
|
{
|
|
// Arrange
|
|
var downloader = new Mock<ISatelliteDownloader>();
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
|
|
var existing = new List<TileEntity>
|
|
{
|
|
new()
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
TileZoom = TestCoordinates.DefaultZoom,
|
|
Latitude = TestCoordinates.TileLat,
|
|
Longitude = TestCoordinates.TileLon,
|
|
Version = DateTime.UtcNow.Year,
|
|
FilePath = "tiles/18/0/0/cached.jpg",
|
|
TileSizePixels = 256,
|
|
ImageType = "jpg",
|
|
Source = TileSourceConverter.ToWireValue(TileSource.GoogleMaps),
|
|
CapturedAt = DateTime.UtcNow,
|
|
},
|
|
};
|
|
tileRepo
|
|
.Setup(r => r.GetTilesByRegionAsync(
|
|
TestCoordinates.TileLat, TestCoordinates.TileLon, 200, TestCoordinates.DefaultZoom))
|
|
.ReturnsAsync(existing);
|
|
|
|
IEnumerable<ExistingTileInfo>? capturedExisting = null;
|
|
downloader
|
|
.Setup(d => d.GetTilesWithMetadataAsync(
|
|
It.IsAny<GeoPoint>(),
|
|
It.IsAny<double>(),
|
|
TestCoordinates.DefaultZoom,
|
|
It.IsAny<IEnumerable<ExistingTileInfo>>(),
|
|
It.IsAny<CancellationToken>()))
|
|
.Callback<GeoPoint, double, int, IEnumerable<ExistingTileInfo>, CancellationToken>(
|
|
(_, _, _, e, _) => capturedExisting = e.ToList())
|
|
.ReturnsAsync(new List<DownloadedTileInfoV2>());
|
|
|
|
var service = BuildService(downloader, tileRepo);
|
|
|
|
// Act
|
|
var result = await service.DownloadAndStoreTilesAsync(
|
|
TestCoordinates.TileLat, TestCoordinates.TileLon, 200, TestCoordinates.DefaultZoom);
|
|
|
|
// Assert
|
|
result.Should().HaveCount(1);
|
|
result[0].Id.Should().Be(existing[0].Id);
|
|
capturedExisting.Should().NotBeNull();
|
|
capturedExisting!.Should().ContainSingle()
|
|
.Which.Should().BeEquivalentTo(new ExistingTileInfo(
|
|
TestCoordinates.TileLat, TestCoordinates.TileLon, TestCoordinates.DefaultZoom));
|
|
tileRepo.Verify(r => r.InsertAsync(It.IsAny<TileEntity>()), Times.Never,
|
|
"cached tile should not be re-inserted");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DownloadAndStoreTilesAsync_TreatsCachedTileFromPriorYearAsFresh_AZ357_AC1()
|
|
{
|
|
// Arrange
|
|
var downloader = new Mock<ISatelliteDownloader>();
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
|
|
var priorYearCached = new List<TileEntity>
|
|
{
|
|
new()
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
TileZoom = TestCoordinates.DefaultZoom,
|
|
Latitude = TestCoordinates.TileLat,
|
|
Longitude = TestCoordinates.TileLon,
|
|
Version = DateTime.UtcNow.Year - 1,
|
|
FilePath = "tiles/18/0/0/cached_prior_year.jpg",
|
|
TileSizePixels = 256,
|
|
ImageType = "jpg",
|
|
Source = TileSourceConverter.ToWireValue(TileSource.GoogleMaps),
|
|
CapturedAt = DateTime.UtcNow.AddYears(-1),
|
|
},
|
|
};
|
|
tileRepo
|
|
.Setup(r => r.GetTilesByRegionAsync(
|
|
It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<int>()))
|
|
.ReturnsAsync(priorYearCached);
|
|
|
|
IEnumerable<ExistingTileInfo>? capturedExisting = null;
|
|
downloader
|
|
.Setup(d => d.GetTilesWithMetadataAsync(
|
|
It.IsAny<GeoPoint>(),
|
|
It.IsAny<double>(),
|
|
It.IsAny<int>(),
|
|
It.IsAny<IEnumerable<ExistingTileInfo>>(),
|
|
It.IsAny<CancellationToken>()))
|
|
.Callback<GeoPoint, double, int, IEnumerable<ExistingTileInfo>, CancellationToken>(
|
|
(_, _, _, e, _) => capturedExisting = e.ToList())
|
|
.ReturnsAsync(new List<DownloadedTileInfoV2>());
|
|
|
|
var service = BuildService(downloader, tileRepo);
|
|
|
|
// Act
|
|
var result = await service.DownloadAndStoreTilesAsync(
|
|
TestCoordinates.TileLat, TestCoordinates.TileLon, 200, TestCoordinates.DefaultZoom);
|
|
|
|
// Assert
|
|
capturedExisting.Should().NotBeNull();
|
|
capturedExisting!.Should().ContainSingle(
|
|
"after AZ-357 the version column no longer gates the cache; the prior-year row is reusable");
|
|
result.Should().HaveCount(1);
|
|
result[0].Id.Should().Be(priorYearCached[0].Id);
|
|
tileRepo.Verify(r => r.InsertAsync(It.IsAny<TileEntity>()), Times.Never,
|
|
"cached tile from prior year must not be re-inserted");
|
|
}
|
|
|
|
[Fact]
|
|
public void BuildTileEntity_DoesNotPopulateVersion_AZ357()
|
|
{
|
|
// Arrange
|
|
var downloader = new Mock<ISatelliteDownloader>();
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
TileEntity? captured = null;
|
|
tileRepo
|
|
.Setup(r => r.InsertAsync(It.IsAny<TileEntity>()))
|
|
.Callback<TileEntity>(e => captured = e)
|
|
.ReturnsAsync(Guid.NewGuid());
|
|
downloader
|
|
.Setup(d => d.DownloadSingleTileAsync(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new DownloadedTileInfoV2(1, 2, 18, 47.46, 37.65, "tiles/18/1/2.jpg", 100.0));
|
|
|
|
var service = BuildService(downloader, tileRepo);
|
|
|
|
// Act
|
|
_ = service.DownloadAndStoreSingleTileAsync(47.46, 37.65, 18).GetAwaiter().GetResult();
|
|
|
|
// Assert
|
|
captured.Should().NotBeNull();
|
|
captured!.Version.Should().BeNull("AZ-357: new code never writes the deprecated year-based version");
|
|
}
|
|
|
|
[Fact]
|
|
public void BuildTileEntity_DoesNotPopulateMapsVersion_AZ373_AC1()
|
|
{
|
|
// Arrange
|
|
var downloader = new Mock<ISatelliteDownloader>();
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
TileEntity? captured = null;
|
|
tileRepo
|
|
.Setup(r => r.InsertAsync(It.IsAny<TileEntity>()))
|
|
.Callback<TileEntity>(e => captured = e)
|
|
.ReturnsAsync(Guid.NewGuid());
|
|
downloader
|
|
.Setup(d => d.DownloadSingleTileAsync(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new DownloadedTileInfoV2(1, 2, 18, 47.46, 37.65, "tiles/18/1/2.jpg", 100.0));
|
|
|
|
var service = BuildService(downloader, tileRepo);
|
|
|
|
// Act
|
|
_ = service.DownloadAndStoreSingleTileAsync(47.46, 37.65, 18).GetAwaiter().GetResult();
|
|
|
|
// Assert
|
|
captured.Should().NotBeNull();
|
|
captured!.MapsVersion.Should().BeNull(
|
|
"AZ-373 option (a): new code never writes a MapsVersion value; the column is retained for forensics on pre-existing rows only");
|
|
}
|
|
|
|
[Fact]
|
|
public void DownloadTileResponse_DoesNotExposeMapsVersion_AZ373_AC2()
|
|
{
|
|
// Act
|
|
var property = typeof(DownloadTileResponse).GetProperty("MapsVersion");
|
|
|
|
// Assert
|
|
property.Should().BeNull(
|
|
"AZ-373 AC-2 option (a): MapsVersion is removed from the HTTP response shape");
|
|
}
|
|
|
|
[Fact]
|
|
public void TileMetadata_DoesNotExposeMapsVersion_AZ373_AC1()
|
|
{
|
|
// Act
|
|
var property = typeof(TileMetadata).GetProperty("MapsVersion");
|
|
|
|
// Assert
|
|
property.Should().BeNull(
|
|
"AZ-373 AC-1: TileMetadata DTO no longer carries MapsVersion (consistent with the HTTP response removal)");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetTileAsync_KnownId_ReturnsMappedMetadata()
|
|
{
|
|
// Arrange
|
|
var id = Guid.NewGuid();
|
|
var entity = new TileEntity
|
|
{
|
|
Id = id,
|
|
TileZoom = 18,
|
|
Latitude = TestCoordinates.TileLat,
|
|
Longitude = TestCoordinates.TileLon,
|
|
TileSizePixels = 256,
|
|
ImageType = "jpg",
|
|
FilePath = "tiles/18/0/0/x.jpg",
|
|
Version = 2026,
|
|
Source = TileSourceConverter.ToWireValue(TileSource.GoogleMaps),
|
|
CapturedAt = DateTime.UtcNow,
|
|
};
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
tileRepo.Setup(r => r.GetByIdAsync(id)).ReturnsAsync(entity);
|
|
var service = BuildService(new Mock<ISatelliteDownloader>(), tileRepo);
|
|
|
|
// Act
|
|
var result = await service.GetTileAsync(id);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
result!.Id.Should().Be(id);
|
|
result.FilePath.Should().Be("tiles/18/0/0/x.jpg");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetTileAsync_UnknownId_ReturnsNull()
|
|
{
|
|
// Arrange
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
tileRepo.Setup(r => r.GetByIdAsync(It.IsAny<Guid>())).ReturnsAsync((TileEntity?)null);
|
|
var service = BuildService(new Mock<ISatelliteDownloader>(), tileRepo);
|
|
|
|
// Act
|
|
var result = await service.GetTileAsync(Guid.NewGuid());
|
|
|
|
// Assert
|
|
result.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetOrDownloadTileAsync_CacheHit_ReturnsCachedBytes_AZ310_AC2()
|
|
{
|
|
// Arrange
|
|
const int z = 18, x = 123, y = 456;
|
|
var downloader = new Mock<ISatelliteDownloader>(MockBehavior.Strict);
|
|
var tileRepo = new Mock<ITileRepository>(MockBehavior.Strict);
|
|
var cache = new MemoryCache(new MemoryCacheOptions());
|
|
var cached = new byte[] { 1, 2, 3, 4 };
|
|
cache.Set($"tile_{z}_{x}_{y}", cached);
|
|
var service = BuildService(downloader, tileRepo, cache);
|
|
|
|
// Act
|
|
var result = await service.GetOrDownloadTileAsync(z, x, y);
|
|
|
|
// Assert
|
|
result.Bytes.Should().BeSameAs(cached);
|
|
result.ContentType.Should().Be("image/jpeg");
|
|
result.ETag.Should().Be($"\"{z}_{x}_{y}\"");
|
|
result.MaxAge.Should().Be(TimeSpan.FromDays(1));
|
|
downloader.VerifyNoOtherCalls();
|
|
tileRepo.VerifyNoOtherCalls();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetOrDownloadTileAsync_RepoHit_ReadsFromDisk_NoDownloader_AZ310_AC3()
|
|
{
|
|
// Arrange
|
|
const int z = 18, x = 100, y = 200;
|
|
var tempPath = Path.Combine(Path.GetTempPath(), $"sp-tests-tile-{Guid.NewGuid():N}.jpg");
|
|
var fileBytes = new byte[] { 9, 8, 7 };
|
|
await File.WriteAllBytesAsync(tempPath, fileBytes);
|
|
|
|
try
|
|
{
|
|
var downloader = new Mock<ISatelliteDownloader>(MockBehavior.Strict);
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
tileRepo
|
|
.Setup(r => r.GetByTileCoordinatesAsync(z, x, y))
|
|
.ReturnsAsync(new TileEntity
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
TileZoom = z,
|
|
TileX = x,
|
|
TileY = y,
|
|
FilePath = tempPath,
|
|
Source = TileSourceConverter.ToWireValue(TileSource.GoogleMaps),
|
|
CapturedAt = DateTime.UtcNow,
|
|
});
|
|
|
|
var service = BuildService(downloader, tileRepo);
|
|
|
|
// Act
|
|
var result = await service.GetOrDownloadTileAsync(z, x, y);
|
|
|
|
// Assert
|
|
result.Bytes.Should().Equal(fileBytes);
|
|
result.ContentType.Should().Be("image/jpeg");
|
|
result.ETag.Should().Be($"\"{z}_{x}_{y}\"");
|
|
tileRepo.Verify(r => r.GetByTileCoordinatesAsync(z, x, y), Times.Once);
|
|
tileRepo.Verify(r => r.InsertAsync(It.IsAny<TileEntity>()), Times.Never);
|
|
downloader.VerifyNoOtherCalls();
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(tempPath);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetOrDownloadTileAsync_DownloaderFallback_InsertsAndReturnsBytes_AZ310_AC4()
|
|
{
|
|
// Arrange
|
|
const int z = 18, x = 50, y = 60;
|
|
var tempPath = Path.Combine(Path.GetTempPath(), $"sp-tests-dl-{Guid.NewGuid():N}.jpg");
|
|
var fileBytes = new byte[] { 5, 5, 5, 5 };
|
|
await File.WriteAllBytesAsync(tempPath, fileBytes);
|
|
|
|
try
|
|
{
|
|
var downloader = new Mock<ISatelliteDownloader>();
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
tileRepo.Setup(r => r.GetByTileCoordinatesAsync(z, x, y)).ReturnsAsync((TileEntity?)null);
|
|
downloader
|
|
.Setup(d => d.DownloadSingleTileAsync(It.IsAny<double>(), It.IsAny<double>(), z, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new DownloadedTileInfoV2(x, y, z, 47.46, 37.65, tempPath, 100.0));
|
|
|
|
var service = BuildService(downloader, tileRepo);
|
|
|
|
// Act
|
|
var result = await service.GetOrDownloadTileAsync(z, x, y);
|
|
|
|
// Assert
|
|
result.Bytes.Should().Equal(fileBytes);
|
|
tileRepo.Verify(r => r.InsertAsync(It.Is<TileEntity>(t =>
|
|
t.TileZoom == z && t.TileX == x && t.TileY == y && t.FilePath == tempPath)), Times.Once);
|
|
downloader.Verify(d => d.DownloadSingleTileAsync(It.IsAny<double>(), It.IsAny<double>(), z, It.IsAny<CancellationToken>()), Times.Once);
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(tempPath);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DownloadAndStoreSingleTileAsync_HappyPath_CallsDownloaderAndRepo_AZ311_AC2()
|
|
{
|
|
// Arrange
|
|
const int zoom = 18;
|
|
var downloader = new Mock<ISatelliteDownloader>();
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
downloader
|
|
.Setup(d => d.DownloadSingleTileAsync(TestCoordinates.TileLat, TestCoordinates.TileLon, zoom, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new DownloadedTileInfoV2(123, 456, zoom, TestCoordinates.TileLat, TestCoordinates.TileLon, "tiles/18/123/456.jpg", 100.0));
|
|
|
|
var service = BuildService(downloader, tileRepo);
|
|
|
|
// Act
|
|
var result = await service.DownloadAndStoreSingleTileAsync(TestCoordinates.TileLat, TestCoordinates.TileLon, zoom);
|
|
|
|
// Assert
|
|
result.TileZoom.Should().Be(zoom);
|
|
result.TileX.Should().Be(123);
|
|
result.TileY.Should().Be(456);
|
|
result.FilePath.Should().Be("tiles/18/123/456.jpg");
|
|
result.TileSizePixels.Should().Be(256);
|
|
result.ImageType.Should().Be("jpg");
|
|
downloader.Verify(d => d.DownloadSingleTileAsync(TestCoordinates.TileLat, TestCoordinates.TileLon, zoom, It.IsAny<CancellationToken>()), Times.Once);
|
|
tileRepo.Verify(r => r.InsertAsync(It.IsAny<TileEntity>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DownloadAndStoreSingleTileAsync_ForwardsCancellationTokenToDownloader_AZ371_AC3()
|
|
{
|
|
// Arrange
|
|
const int zoom = 18;
|
|
var downloader = new Mock<ISatelliteDownloader>();
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
CancellationToken capturedToken = default;
|
|
downloader
|
|
.Setup(d => d.DownloadSingleTileAsync(It.IsAny<double>(), It.IsAny<double>(), zoom, It.IsAny<CancellationToken>()))
|
|
.Callback<double, double, int, CancellationToken>((_, _, _, ct) => capturedToken = ct)
|
|
.ReturnsAsync(new DownloadedTileInfoV2(1, 2, zoom, 0, 0, "p.jpg", 100.0));
|
|
var service = BuildService(downloader, tileRepo);
|
|
using var cts = new CancellationTokenSource();
|
|
|
|
// Act
|
|
await service.DownloadAndStoreSingleTileAsync(0, 0, zoom, cts.Token);
|
|
|
|
// Assert
|
|
capturedToken.Should().Be(cts.Token, "AZ-371 AC-3: caller-supplied CT must reach the downloader");
|
|
}
|
|
|
|
[Fact]
|
|
public void BuildTileEntity_SetsGoogleMapsSourceAndUtcCapturedAt_AZ484_AC5()
|
|
{
|
|
// Arrange
|
|
var downloader = new Mock<ISatelliteDownloader>();
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
TileEntity? captured = null;
|
|
tileRepo
|
|
.Setup(r => r.InsertAsync(It.IsAny<TileEntity>()))
|
|
.Callback<TileEntity>(e => captured = e)
|
|
.ReturnsAsync(Guid.NewGuid());
|
|
downloader
|
|
.Setup(d => d.DownloadSingleTileAsync(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new DownloadedTileInfoV2(1, 2, 18, 47.46, 37.65, "tiles/18/1/2.jpg", 100.0));
|
|
|
|
var service = BuildService(downloader, tileRepo);
|
|
var before = DateTime.UtcNow;
|
|
|
|
// Act
|
|
_ = service.DownloadAndStoreSingleTileAsync(47.46, 37.65, 18).GetAwaiter().GetResult();
|
|
|
|
// Assert
|
|
var after = DateTime.UtcNow;
|
|
captured.Should().NotBeNull();
|
|
captured!.Source.Should().Be(TileSourceConverter.ToWireValue(TileSource.GoogleMaps),
|
|
"AZ-484 AC-5: the Google Maps download path stamps Source='google_maps' (contract wire value)");
|
|
captured.CapturedAt.Kind.Should().NotBe(DateTimeKind.Local,
|
|
"captured_at must be UTC per the v1.0.0 storage contract");
|
|
captured.CapturedAt.Should().BeOnOrAfter(before).And.BeOnOrBefore(after,
|
|
"AZ-484 AC-5: CapturedAt is the UtcNow at download time");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DownloadAndStoreSingleTileAsync_DownloaderThrows_DoesNotInsert_AZ311_AC2b()
|
|
{
|
|
// Arrange
|
|
var downloader = new Mock<ISatelliteDownloader>();
|
|
var tileRepo = new Mock<ITileRepository>();
|
|
downloader
|
|
.Setup(d => d.DownloadSingleTileAsync(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
|
.ThrowsAsync(new InvalidOperationException("network down"));
|
|
|
|
var service = BuildService(downloader, tileRepo);
|
|
|
|
// Act
|
|
Func<Task> act = () => service.DownloadAndStoreSingleTileAsync(TestCoordinates.TileLat, TestCoordinates.TileLon, 18);
|
|
|
|
// Assert
|
|
await act.Should().ThrowAsync<InvalidOperationException>().WithMessage("network down");
|
|
tileRepo.Verify(r => r.InsertAsync(It.IsAny<TileEntity>()), Times.Never);
|
|
}
|
|
}
|
|
|
|
public class TileDownloaderRegistrationTests
|
|
{
|
|
[Fact]
|
|
public void AddTileDownloader_RegistersNamedGoogleMapsTilesHttpClient_AZ374_AC1()
|
|
{
|
|
// Arrange
|
|
var services = new ServiceCollection();
|
|
|
|
// Act
|
|
services.AddTileDownloader();
|
|
using var provider = services.BuildServiceProvider();
|
|
var factory = provider.GetRequiredService<IHttpClientFactory>();
|
|
using var client = factory.CreateClient("GoogleMapsTiles");
|
|
|
|
// Assert
|
|
client.DefaultRequestHeaders.UserAgent.Should().NotBeEmpty(
|
|
"AZ-374 AC-1: the named GoogleMapsTiles client must carry the User-Agent set once at registration time");
|
|
client.DefaultRequestHeaders.UserAgent.ToString()
|
|
.Should().Contain("Mozilla/5.0", "preserves the existing outbound User-Agent for Google Maps");
|
|
client.Timeout.Should().Be(TimeSpan.FromSeconds(100),
|
|
"preserves the HttpClient default (100s) until C18 wires this to MapConfig");
|
|
}
|
|
}
|
|
|
|
public class GoogleMapsDownloaderZoomValidationTests
|
|
{
|
|
private static GoogleMapsDownloaderV2 BuildDownloader()
|
|
{
|
|
var mapConfig = Options.Create(new MapConfig { Service = "googlemaps", ApiKey = "test-key" });
|
|
var storageConfig = Options.Create(new StorageConfig
|
|
{
|
|
TilesDirectory = Path.Combine(Path.GetTempPath(), "sp-tests-tiles"),
|
|
ReadyDirectory = Path.Combine(Path.GetTempPath(), "sp-tests-ready"),
|
|
});
|
|
var processingConfig = Options.Create(new ProcessingConfig());
|
|
var httpClientFactory = new Mock<IHttpClientFactory>().Object;
|
|
|
|
return new GoogleMapsDownloaderV2(
|
|
NullLogger<GoogleMapsDownloaderV2>.Instance,
|
|
mapConfig,
|
|
storageConfig,
|
|
processingConfig,
|
|
httpClientFactory);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(25)]
|
|
[InlineData(14)]
|
|
[InlineData(0)]
|
|
[InlineData(-1)]
|
|
public async Task DownloadSingleTileAsync_RejectsZoomLevelOutsideAllowedRange_BTN02(int invalidZoom)
|
|
{
|
|
// Arrange
|
|
var downloader = BuildDownloader();
|
|
|
|
// Act
|
|
Func<Task> act = () => downloader.DownloadSingleTileAsync(TestCoordinates.TileLat, TestCoordinates.TileLon, invalidZoom);
|
|
|
|
// Assert
|
|
await act.Should().ThrowAsync<ArgumentException>()
|
|
.Where(ex => ex.ParamName == "zoomLevel" && ex.Message.Contains("not allowed"));
|
|
}
|
|
}
|