Skip to content

Commit

Permalink
Merge branch 'release/2.0.1' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhykos committed Nov 1, 2024
2 parents 2b6ac60 + 22fdfaa commit 8261011
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deno-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Archive Linux binary x86_64
uses: actions/upload-artifact@v4
with:
name: autophoto-linux-x86_64-2.0.0
name: autophoto-linux-x86_64-2.0.1
path: build/linux-x86_64

- name: Build Linux binary ARM
Expand All @@ -40,5 +40,5 @@ jobs:
- name: Archive Linux binary ARM
uses: actions/upload-artifact@v4
with:
name: autophoto-linux-arm-2.0.0
name: autophoto-linux-arm-2.0.1
path: build/linux-arm
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules/
*.env
/build/
cov.lcov
/temp/
9 changes: 5 additions & 4 deletions src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const runScanner = async (
const relationRepository = new KvRelationRepository(kvDriver);

const scanner = new Scanner(
new KvImageRepository(kvDriver),
new KvImageRepository(kvDriver, logger),
videoGameRepository,
relationRepository,
);
Expand Down Expand Up @@ -102,11 +102,12 @@ export async function scan(
} catch (error) {
// TODO Alerting
hasError = true;
logger.error("An error occurred while scanning.");
logger.error(error);
logger.error("An error occurred while scanning:");
logger.error((error as Error).message);
console.error(error);
}

if (hasError) {
throw new Error("An error occurred while scanning.");
throw new Error("An error occurred while scanning (see console).");
}
}
1 change: 1 addition & 0 deletions src/scanner/domain/entity/VideoGameScreenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export class VideoGameScreenshot extends Entity {
constructor(
public readonly image: Image,
uuid?: string,
public readonly toUpdate = false,
) {
super(uuid);
}
Expand Down
25 changes: 16 additions & 9 deletions src/scanner/repository/ImageRepository.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Log } from "@cross/log";
import type { KvDriver } from "../../common/dbdriver/KvDriver.ts";
import { Directory } from "../../common/domain/valueobject/Directory.ts";
import { File } from "../../common/domain/valueobject/File.ts";
Expand All @@ -15,7 +16,10 @@ export interface ImageRepository {
export class KvImageRepository implements ImageRepository {
private readonly commonRepository: CommonKvImageRepository;

constructor(kvDriver: KvDriver) {
constructor(
kvDriver: KvDriver,
private readonly logger: Log,
) {
this.commonRepository = new CommonKvImageRepository(kvDriver);
}

Expand All @@ -37,22 +41,25 @@ export class KvImageRepository implements ImageRepository {
}

async getAllVideoGameScreenshots(): Promise<VideoGameScreenshot[]> {
const entities: ImageRepositoryRepositoryEntity[] =
const dbEntities: ImageRepositoryRepositoryEntity[] =
await this.commonRepository.getAllVideoGameScreenshots();

return entities.map((entity) => {
const file = new File(new Path(entity.path));
return dbEntities.map((dbEntity) => {
const file = new File(new Path(dbEntity.path));

let toUpdate = false;
const repositoryChecksum: string = file.getChecksum();
if (repositoryChecksum !== entity.checksum) {
throw new Error(
`Checksum mismatch for file "${file.path.value}": expected "${entity.checksum}" (into repository), got "${repositoryChecksum}" (from file)`,
if (repositoryChecksum !== dbEntity.checksum) {
toUpdate = true;
this.logger.warn(
`Checksum mismatch for file "${file.path.value}": expected "${dbEntity.checksum}" (into repository), got "${repositoryChecksum}" (from file)`,
);
}

return new VideoGameScreenshot(
new Image(new Directory(new Path(entity.scanRootDirectory)), file),
entity.uuid,
new Image(new Directory(new Path(dbEntity.scanRootDirectory)), file),
dbEntity.uuid,
toUpdate,
);
});
}
Expand Down
32 changes: 22 additions & 10 deletions src/scanner/service/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,41 @@ export class Scanner {
imageDirectory: ImageDirectory,
): Promise<void> {
const newSavedScreenshots: VideoGameScreenshot[] =
await this.saveNewScreenshots(imageDirectory);
await this.saveScreenshots(imageDirectory);
await this.saveNewVideoGames(imageDirectory, newSavedScreenshots);
await this.saveNewLinks(imageDirectory, newSavedScreenshots);
}

private async saveNewScreenshots(
private async saveScreenshots(
imageDirectory: ImageDirectory,
): Promise<VideoGameScreenshot[]> {
const scannedImages: Image[] = await imageDirectory.scan();

const repositoryScreenshots: VideoGameScreenshot[] =
await this.imageRepository.getAllVideoGameScreenshots();

const newScreenshotsToSave: VideoGameScreenshot[] = scannedImages
.filter(
(image) =>
!repositoryScreenshots.some((screenshot) =>
screenshot.image.equals(image),
),
)
.map((image) => new VideoGameScreenshot(image));
const newScreenshotsToSave: VideoGameScreenshot[] = [];
const screenshotsToUpdate: VideoGameScreenshot[] = [];

for (const image of scannedImages) {
const existingScreenshot: VideoGameScreenshot | undefined =
repositoryScreenshots.find((screenshot) =>
screenshot.image.equals(image),
);

if (existingScreenshot) {
if (existingScreenshot.toUpdate === true) {
screenshotsToUpdate.push(
new VideoGameScreenshot(image, existingScreenshot.id),
);
}
} else {
newScreenshotsToSave.push(new VideoGameScreenshot(image));
}
}

await this.imageRepository.saveVideoGameScreenshots(newScreenshotsToSave);
await this.imageRepository.saveVideoGameScreenshots(screenshotsToUpdate);

return newScreenshotsToSave;
}
Expand Down
15 changes: 9 additions & 6 deletions test/mock/logger/MockLoggerTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import { LogTransportBase, Severity } from "@cross/log";

export class MockLoggerTransport extends LogTransportBase {
errorMessages: string[] = [];
logMessages: string[] = [];
warningMessages: string[] = [];
infoMessages: string[] = [];

log(level: Severity, scope: string, data: unknown[], timestamp: Date) {
if (super.shouldLog(level)) {
const formattedMessage = `${timestamp.toISOString()} ${level} ${scope} ${data.join(
" ",
)}`;
const message: string = data.join(" ");
const formattedMessage = `${timestamp.toISOString()} ${level} ${scope} ${message}`;

if (level === Severity.Error) {
this.errorMessages.push(formattedMessage);
this.errorMessages.push(message);
console.error(formattedMessage);
} else if (level === Severity.Warn) {
this.warningMessages.push(message);
console.warn(formattedMessage);
} else {
this.logMessages.push(formattedMessage);
this.infoMessages.push(message);
console.log(formattedMessage);
}
}
Expand Down
8 changes: 6 additions & 2 deletions test/mock/logger/mockLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ export class MockLogger {
return new Log([this.transport]);
}

get logMessages(): string[] {
return this.transport.logMessages;
get infoMessages(): string[] {
return this.transport.infoMessages;
}

get errorMessages(): string[] {
return this.transport.errorMessages;
}

get warningMessages(): string[] {
return this.transport.warningMessages;
}
}

export const mockLogger = (): Log => {
Expand Down
10 changes: 5 additions & 5 deletions test/prescan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ describe("main prescan", () => {
const result: boolean = preScan(configuration, logger.logger());

assert(result);
assertEquals(logger.logMessages.length, 4);
assertEquals(logger.infoMessages.length, 4);
assertEquals(logger.errorMessages.length, 0);
assertContainsMatch(
logger.logMessages,
logger.infoMessages,
/Pre-scanning .+\/video-game\.\.\./,
);
});
Expand All @@ -51,7 +51,7 @@ describe("main prescan", () => {
const result: boolean = preScan(configuration, new Log([loggerTransport]));

assertFalse(result);
assertEquals(loggerTransport.logMessages.length, 3);
assertEquals(loggerTransport.infoMessages.length, 3);
assertEquals(loggerTransport.errorMessages.length, 2);
assertContainsMatch(
loggerTransport.errorMessages,
Expand All @@ -76,7 +76,7 @@ describe("main prescan", () => {
const result: boolean = preScan(configuration, new Log([loggerTransport]));

assertFalse(result);
assertEquals(loggerTransport.logMessages.length, 3);
assertEquals(loggerTransport.infoMessages.length, 3);
assertEquals(loggerTransport.errorMessages.length, 2);
assertContainsMatch(
loggerTransport.errorMessages,
Expand All @@ -101,7 +101,7 @@ describe("main prescan", () => {
const result: boolean = preScan(configuration, new Log([loggerTransport]));

assertFalse(result);
assertEquals(loggerTransport.logMessages.length, 3);
assertEquals(loggerTransport.infoMessages.length, 3);
assertEquals(loggerTransport.errorMessages.length, 2);
assertContainsMatch(
loggerTransport.errorMessages,
Expand Down
5 changes: 4 additions & 1 deletion test/scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ describe("main scanner", () => {
);

assert(error instanceof Error);
assertEquals(error.message, "An error occurred while scanning.");
assertEquals(
error.message,
"An error occurred while scanning (see console).",
);
});

it("should debug scan", async () => {
Expand Down
16 changes: 9 additions & 7 deletions test/scanner/repository/KvImageRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { assert, assertEquals, assertRejects } from "@std/assert";
import { assertEquals } from "@std/assert";
import { describe, it } from "@std/testing/bdd";
import { KvDriver } from "../../../src/common/dbdriver/KvDriver.ts";
import type { ImageRepositoryRepositoryEntity } from "../../../src/common/repository/entity/ImageRepositoryRepositoryEntity.ts";
import { KvImageRepository } from "../../../src/scanner/repository/ImageRepository.ts";
import { pathExists } from "../../../src/utils/file.ts";
import { MockLogger } from "../../mock/logger/mockLogger.ts";

const tempDatabaseFilePath = "./test/it-database.sqlite3";

Expand All @@ -13,8 +14,9 @@ describe("KvImageRepository", () => {
Deno.removeSync(tempDatabaseFilePath);
}

const logger = new MockLogger();
const kvDriver = new KvDriver(tempDatabaseFilePath);
const repository = new KvImageRepository(kvDriver);
const repository = new KvImageRepository(kvDriver, logger.logger());

try {
assertEquals(await repository.getAllVideoGameScreenshots(), []);
Expand All @@ -26,13 +28,13 @@ describe("KvImageRepository", () => {
checksum: "toto",
} satisfies ImageRepositoryRepositoryEntity);

const error = await assertRejects(async () => {
await repository.getAllVideoGameScreenshots();
});
await repository.getAllVideoGameScreenshots();

assert(error instanceof Error);
assertEquals(logger.errorMessages.length, 0);
assertEquals(logger.infoMessages.length, 0);
assertEquals(logger.warningMessages.length, 1);
assertEquals(
error.message,
logger.warningMessages[0],
'Checksum mismatch for file "test/resources/video-game/8-Bit Bayonetta (2015)/PC/8-Bit Bayonetta - 00001.webp": expected "toto" (into repository), got "0c58763a36f41a2c1808fd0f8dc138c21f9fc32eef674c884b286e32658649f902b0d74c7cb4086b9cbcf3871062cfaee96819d7e881e4d1440f16608308c779" (from file)',
);
} finally {
Expand Down
Loading

0 comments on commit 8261011

Please sign in to comment.