Skip to content

Commit

Permalink
chore: e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 committed Mar 22, 2024
1 parent 40632ae commit f2cbae7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
5 changes: 2 additions & 3 deletions cli/src/commands/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import byteSize from 'byte-size';
import { Presets, SingleBar } from 'cli-progress';
import { chunk } from 'lodash-es';
import { Stats, createReadStream } from 'node:fs';
import { access, constants, stat, unlink } from 'node:fs/promises';
import { stat, unlink } from 'node:fs/promises';
import os from 'node:os';
import path, { basename } from 'node:path';
import { BaseOptions, authenticate, crawl, sha1 } from 'src/utils';
Expand Down Expand Up @@ -115,7 +115,7 @@ const checkForDuplicates = async (files: string[], { concurrency }: UploadOption
progressBar.stop();
}

console.log(`Found ${newFiles.length} new files and ${duplicates.length} duplicates`);
console.log(`Found ${newFiles.length} new files and ${duplicates.length} duplicate${s(duplicates.length)}`);

return { newFiles, duplicates };
};
Expand Down Expand Up @@ -180,7 +180,6 @@ const uploadFile = async (input: string, stats: Stats): Promise<AssetFileUploadR
// XMP sidecars can come in two filename formats. For a photo named photo.ext, the filenames are photo.ext.xmp and photo.xmp
[`${noExtension}.xmp`, `${input}.xmp`].map(async (sidecarPath) => {
try {
await access(sidecarPath, constants.R_OK);
const stats = await stat(sidecarPath);
return new UploadFile(sidecarPath, stats.size);
} catch {
Expand Down
3 changes: 2 additions & 1 deletion e2e/src/cli/specs/server-info.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { beforeAll, describe, expect, it } from 'vitest';
describe(`immich server-info`, () => {
beforeAll(async () => {
await utils.resetDatabase();
await utils.cliLogin();
const admin = await utils.adminSetup();
await utils.cliLogin(admin.accessToken);
});

it('should return the server info', async () => {
Expand Down
39 changes: 37 additions & 2 deletions e2e/src/cli/specs/upload.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { getAllAlbums, getAllAssets } from '@immich/sdk';
import { LoginResponseDto, getAllAlbums, getAllAssets } from '@immich/sdk';
import { mkdir, readdir, rm, symlink } from 'node:fs/promises';
import { asKeyAuth, immichCli, testAssetDir, utils } from 'src/utils';
import { beforeAll, beforeEach, describe, expect, it } from 'vitest';

describe(`immich upload`, () => {
let admin: LoginResponseDto;
let key: string;

beforeAll(async () => {
await utils.resetDatabase();
key = await utils.cliLogin();

admin = await utils.adminSetup();
key = await utils.cliLogin(admin.accessToken);
});

beforeEach(async () => {
Expand All @@ -27,6 +30,38 @@ describe(`immich upload`, () => {
const assets = await getAllAssets({}, { headers: asKeyAuth(key) });
expect(assets.length).toBe(1);
});

it('should skip a duplicate file', async () => {
const first = await immichCli(['upload', `${testAssetDir}/albums/nature/silver_fir.jpg`]);
expect(first.stderr).toBe('');
expect(first.stdout.split('\n')).toEqual(
expect.arrayContaining([expect.stringContaining('Successfully uploaded 1 asset')]),
);
expect(first.exitCode).toBe(0);

const assets = await getAllAssets({}, { headers: asKeyAuth(key) });
expect(assets.length).toBe(1);

const second = await immichCli(['upload', `${testAssetDir}/albums/nature/silver_fir.jpg`]);
expect(second.stderr).toBe('');
expect(second.stdout.split('\n')).toEqual(
expect.arrayContaining([
expect.stringContaining('Found 0 new files and 1 duplicate'),
expect.stringContaining('All assets were already uploaded, nothing to do'),
]),
);
expect(first.exitCode).toBe(0);
});

it('should skip files that do not exist', async () => {
const { stderr, stdout, exitCode } = await immichCli(['upload', `/path/to/file`]);
expect(stderr).toBe('');
expect(stdout.split('\n')).toEqual(expect.arrayContaining([expect.stringContaining('No files found, exiting')]));
expect(exitCode).toBe(0);

const assets = await getAllAssets({}, { headers: asKeyAuth(key) });
expect(assets.length).toBe(0);
});
});

describe('immich upload --recursive', () => {
Expand Down
5 changes: 2 additions & 3 deletions e2e/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,8 @@ export const utils = {
},
]),

cliLogin: async () => {
const admin = await utils.adminSetup();
const key = await utils.createApiKey(admin.accessToken);
cliLogin: async (accessToken: string) => {
const key = await utils.createApiKey(accessToken);
await immichCli(['login-key', app, `${key.secret}`]);
return key.secret;
},
Expand Down

0 comments on commit f2cbae7

Please sign in to comment.