Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88a6ce5b6b | ||
|
|
41d3e6f72f | ||
|
|
6e361ace8a |
No files matched your search
@@ -172,21 +172,14 @@ Log in once with `~/bin/quak`, as the user the cron job will run as:
|
|||||||
|
|
||||||
The session is saved in that user's data directory (see "The session file").
|
The session is saved in that user's data directory (see "The session file").
|
||||||
Then add the backup to that user's crontab with `crontab -e`. These two lines
|
Then add the backup to that user's crontab with `crontab -e`. These two lines
|
||||||
back up the account to `~/photos-backup` at 03:30 every night and send all
|
back up the account to `~/photos-backup` at 03:30 every night and append all
|
||||||
output to the system log with `logger`. Sunday's run adds `--verify`:
|
output to `~/quak-backup.log`. Sunday's run adds `--verify`:
|
||||||
|
|
||||||
```
|
```
|
||||||
30 3 * * 1-6 $HOME/bin/quak backup $HOME/photos-backup 2>&1 | logger -t quak
|
30 3 * * 1-6 $HOME/bin/quak backup $HOME/photos-backup >> $HOME/quak-backup.log 2>&1
|
||||||
30 3 * * 0 $HOME/bin/quak backup --verify $HOME/photos-backup 2>&1 | logger -t quak
|
30 3 * * 0 $HOME/bin/quak backup --verify $HOME/photos-backup >> $HOME/quak-backup.log 2>&1
|
||||||
```
|
```
|
||||||
|
|
||||||
On Linux with systemd, `logger` writes to the journal, and `journalctl -t quak`
|
|
||||||
shows the runs. The system log adds the time to each line.
|
|
||||||
|
|
||||||
To keep the output in a file instead, end each line with
|
|
||||||
`>> $HOME/quak-backup.log 2>&1` in place of the pipe. Each run's `Started at`
|
|
||||||
and `Finished at` lines then show when it ran.
|
|
||||||
|
|
||||||
A run with `--verify` does all a plain run does. It also hashes each original
|
A run with `--verify` does all a plain run does. It also hashes each original
|
||||||
already in the backup and compares the result with the content hash. The content
|
already in the backup and compares the result with the content hash. The content
|
||||||
hash is the hash of the file's bytes that Ente recorded at upload. The run
|
hash is the hash of the file's bytes that Ente recorded at upload. The run
|
||||||
@@ -283,8 +276,13 @@ factor at that point, login fails.
|
|||||||
|
|
||||||
On success it logs `Logged in as <email> (user <id>)` and
|
On success it logs `Logged in as <email> (user <id>)` and
|
||||||
`Session saved to <path>`. On failure it logs `Login failed: <reason>`, and the
|
`Session saved to <path>`. On failure it logs `Login failed: <reason>`, and the
|
||||||
exit status is 1. `login` is the only command that prompts, and its prompts go
|
exit status is 1. Two reasons are given in words:
|
||||||
to stdout.
|
|
||||||
|
- a password the server refuses: `Login failed: incorrect email or password`
|
||||||
|
- an email with no account on the server:
|
||||||
|
`Login failed: no account with this email on the server`
|
||||||
|
|
||||||
|
`login` is the only command that prompts, and its prompts go to stdout.
|
||||||
|
|
||||||
#### `quak whoami`
|
#### `quak whoami`
|
||||||
|
|
||||||
@@ -370,12 +368,10 @@ once, however many collections hold it. An original the cache already holds is
|
|||||||
copied from there instead.
|
copied from there instead.
|
||||||
|
|
||||||
Progress goes to stderr: one line per original fetched, and a `FAILED` line for
|
Progress goes to stderr: one line per original fetched, and a `FAILED` line for
|
||||||
each original or album that failed. The run starts with `Starting backup...` and
|
each original or album that failed. The run ends with a summary of counts and,
|
||||||
a `Started at` line giving the local date and time, such as
|
when any file failed, a `Failed files:` list of every failed file. With
|
||||||
`Started at 2026-11-11 03:30:00 +0100`. It ends with a summary: a `Finished at`
|
`--json`, the summary and the list are replaced by one JSON object on stdout.
|
||||||
line in the same form, the counts and, when any file failed, a `Failed files:`
|
Its fields are:
|
||||||
list of every failed file. With `--json`, the summary and the list are replaced
|
|
||||||
by one JSON object on stdout. Its fields are:
|
|
||||||
|
|
||||||
- `totalFiles`: how many distinct files the run covered, however many
|
- `totalFiles`: how many distinct files the run covered, however many
|
||||||
collections hold each;
|
collections hold each;
|
||||||
@@ -564,12 +560,12 @@ output, run `yarn --silent quak files --json` or
|
|||||||
`node dist/bin/quak.js files --json` instead.
|
`node dist/bin/quak.js files --json` instead.
|
||||||
|
|
||||||
With `--json`, a command's JSON on stdout takes the place of its summary on
|
With `--json`, a command's JSON on stdout takes the place of its summary on
|
||||||
stderr. For `backup`, that is the `--- Backup complete ---` line, the
|
stderr. For `backup`, that is the `--- Backup complete ---` line, the counts and
|
||||||
`Finished at` line, the counts and the `Failed files:` list. For
|
the `Failed files:` list. For `list-missing-thumbnails`, it is
|
||||||
`list-missing-thumbnails`, it is `No missing thumbnails found.` or
|
`No missing thumbnails found.` or `<n> file(s) with missing thumbnails:`. For
|
||||||
`<n> file(s) with missing thumbnails:`. For `fix-missing-thumbnails`, it is the
|
`fix-missing-thumbnails`, it is the `--- Done ---` line, the counts, the
|
||||||
`--- Done ---` line, the counts, the `Skipped:` list and the `Failed files:`
|
`Skipped:` list and the `Failed files:` list. Every other line is logged as
|
||||||
list. Every other line is logged as without `--json`.
|
without `--json`.
|
||||||
|
|
||||||
### Exit codes
|
### Exit codes
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,6 @@ const context = (): CliContext => ({
|
|||||||
}),
|
}),
|
||||||
prompt: (message) => input({ message }),
|
prompt: (message) => input({ message }),
|
||||||
promptSecret: (message) => password({ message, mask: true }),
|
promptSecret: (message) => password({ message, mask: true }),
|
||||||
now: () => new Date(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const run = (command: Promise<number>): Promise<void> =>
|
const run = (command: Promise<number>): Promise<void> =>
|
||||||
|
|||||||
+45
-11
@@ -1,5 +1,5 @@
|
|||||||
import { SRP, SrpClient } from "fast-srp-hap";
|
import { SRP, SrpClient } from "fast-srp-hap";
|
||||||
import type { ApiClient } from "../api/client.js";
|
import { ApiError, type ApiClient } from "../api/client.js";
|
||||||
import { deriveKEK, deriveLoginSubkey, fromBase64 } from "../crypto/index.js";
|
import { deriveKEK, deriveLoginSubkey, fromBase64 } from "../crypto/index.js";
|
||||||
import type {
|
import type {
|
||||||
AuthorizationResponse,
|
AuthorizationResponse,
|
||||||
@@ -10,6 +10,14 @@ import type {
|
|||||||
// Ente uses the 4096-bit SRP group. Both client and server must agree.
|
// Ente uses the 4096-bit SRP group. Both client and server must agree.
|
||||||
const SRP_PARAMS = SRP.params["4096"];
|
const SRP_PARAMS = SRP.params["4096"];
|
||||||
|
|
||||||
|
// The same error, with a message that says in words what the server refused.
|
||||||
|
const withMessage = (err: ApiError, message: string): ApiError =>
|
||||||
|
new ApiError(message, err.status, {
|
||||||
|
code: err.code,
|
||||||
|
requestID: err.requestID,
|
||||||
|
body: err.body,
|
||||||
|
});
|
||||||
|
|
||||||
// Wrap fast-srp-hap's callback-based genKey into a Promise.
|
// Wrap fast-srp-hap's callback-based genKey into a Promise.
|
||||||
const genKey = (): Promise<Buffer> => SRP.genKey();
|
const genKey = (): Promise<Buffer> => SRP.genKey();
|
||||||
|
|
||||||
@@ -63,13 +71,20 @@ const srpHandshake = async (
|
|||||||
srpClient.setB(Buffer.from(srpB, "base64"));
|
srpClient.setB(Buffer.from(srpB, "base64"));
|
||||||
const srpM1 = srpClient.computeM1().toString("base64");
|
const srpM1 = srpClient.computeM1().toString("base64");
|
||||||
|
|
||||||
const verifyResponse = await api.postJSON<
|
type VerifyResponse = AuthorizationResponse & { srpM2: string };
|
||||||
AuthorizationResponse & { srpM2: string }
|
let verifyResponse: VerifyResponse;
|
||||||
>("/users/srp/verify-session", {
|
try {
|
||||||
sessionID,
|
verifyResponse = await api.postJSON<VerifyResponse>(
|
||||||
srpUserID: srpAttributes.srpUserID,
|
"/users/srp/verify-session",
|
||||||
srpM1,
|
{ sessionID, srpUserID: srpAttributes.srpUserID, srpM1 },
|
||||||
});
|
);
|
||||||
|
} catch (err) {
|
||||||
|
// The server answers 401 when the password does not match.
|
||||||
|
if (err instanceof ApiError && err.status === 401) {
|
||||||
|
throw withMessage(err, "incorrect email or password");
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
srpClient.checkM2(Buffer.from(verifyResponse.srpM2, "base64"));
|
srpClient.checkM2(Buffer.from(verifyResponse.srpM2, "base64"));
|
||||||
|
|
||||||
@@ -101,9 +116,28 @@ export const beginLogin = async (
|
|||||||
email: string,
|
email: string,
|
||||||
password: string,
|
password: string,
|
||||||
): Promise<LoginChallenge> => {
|
): Promise<LoginChallenge> => {
|
||||||
const { attributes } = await api.getJSON<{
|
let attributes: SRPAttributes;
|
||||||
attributes: SRPAttributes;
|
try {
|
||||||
}>("/users/srp/attributes", { email });
|
({ attributes } = await api.getJSON<{ attributes: SRPAttributes }>(
|
||||||
|
"/users/srp/attributes",
|
||||||
|
{ email },
|
||||||
|
));
|
||||||
|
} catch (err) {
|
||||||
|
// The Ente server answers 404 with `{}` when no account has the email.
|
||||||
|
// It answers 404 with the code NOT_FOUND when the account exists but
|
||||||
|
// has no SRP attributes. A server address that points somewhere else
|
||||||
|
// answers 404 without JSON. Those two errors stay as they are.
|
||||||
|
if (
|
||||||
|
err instanceof ApiError &&
|
||||||
|
err.status === 404 &&
|
||||||
|
typeof err.body === "object" &&
|
||||||
|
err.body !== null &&
|
||||||
|
err.code === undefined
|
||||||
|
) {
|
||||||
|
throw withMessage(err, "no account with this email on the server");
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
if (attributes.isEmailMFAEnabled) {
|
if (attributes.isEmailMFAEnabled) {
|
||||||
return { kind: "emailOTP" };
|
return { kind: "emailOTP" };
|
||||||
|
|||||||
@@ -68,29 +68,11 @@ export interface CliContext {
|
|||||||
prompt: (message: string) => Promise<string>;
|
prompt: (message: string) => Promise<string>;
|
||||||
// Like `prompt`, but the answer is masked as it is typed.
|
// Like `prompt`, but the answer is masked as it is typed.
|
||||||
promptSecret: (message: string) => Promise<string>;
|
promptSecret: (message: string) => Promise<string>;
|
||||||
// The current time. The CLI passes `() => new Date()`; tests pass fixed
|
|
||||||
// times.
|
|
||||||
now: () => Date;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sessionPath = (ctx: CliContext): string =>
|
const sessionPath = (ctx: CliContext): string =>
|
||||||
join(ctx.sessionDir, "session.json");
|
join(ctx.sessionDir, "session.json");
|
||||||
|
|
||||||
const pad = (n: number): string => String(n).padStart(2, "0");
|
|
||||||
|
|
||||||
// `date` in local time with its offset from UTC, such as
|
|
||||||
// `2026-11-11 03:30:00 +0100`.
|
|
||||||
const localTime = (date: Date): string => {
|
|
||||||
const offset = -date.getTimezoneOffset();
|
|
||||||
const sign = offset < 0 ? "-" : "+";
|
|
||||||
const abs = Math.abs(offset);
|
|
||||||
return (
|
|
||||||
`${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` +
|
|
||||||
`${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())} ` +
|
|
||||||
`${sign}${pad(Math.floor(abs / 60))}${pad(abs % 60)}`
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Write the session readable by its owner only, in a directory only its owner
|
// Write the session readable by its owner only, in a directory only its owner
|
||||||
// can enter.
|
// can enter.
|
||||||
export const saveSession = (
|
export const saveSession = (
|
||||||
@@ -486,7 +468,6 @@ export const backupCommand = async (
|
|||||||
if (!client) return 3;
|
if (!client) return 3;
|
||||||
|
|
||||||
ctx.log("info", "Starting backup...");
|
ctx.log("info", "Starting backup...");
|
||||||
ctx.log("info", `Started at ${localTime(ctx.now())}`);
|
|
||||||
// The lock is taken before the library opens and starts its refresh, so a
|
// The lock is taken before the library opens and starts its refresh, so a
|
||||||
// run refused while another backup of `dir` runs sends no request.
|
// run refused while another backup of `dir` runs sends no request.
|
||||||
let lock: BackupLock;
|
let lock: BackupLock;
|
||||||
@@ -523,7 +504,6 @@ export const backupCommand = async (
|
|||||||
ctx.stdout.write(JSON.stringify(result, null, 2) + "\n");
|
ctx.stdout.write(JSON.stringify(result, null, 2) + "\n");
|
||||||
} else {
|
} else {
|
||||||
ctx.log("info", "\n--- Backup complete ---");
|
ctx.log("info", "\n--- Backup complete ---");
|
||||||
ctx.log("info", `Finished at ${localTime(ctx.now())}`);
|
|
||||||
ctx.log("info", ` Total files: ${result.totalFiles}`);
|
ctx.log("info", ` Total files: ${result.totalFiles}`);
|
||||||
ctx.log("info", ` Downloaded: ${result.downloaded}`);
|
ctx.log("info", ` Downloaded: ${result.downloaded}`);
|
||||||
ctx.log("info", ` Skipped: ${result.skipped}`);
|
ctx.log("info", ` Skipped: ${result.skipped}`);
|
||||||
|
|||||||
+79
-5
@@ -30,7 +30,7 @@
|
|||||||
import sodium from "libsodium-wrappers-sumo";
|
import sodium from "libsodium-wrappers-sumo";
|
||||||
import { SRP, SrpServer } from "fast-srp-hap";
|
import { SRP, SrpServer } from "fast-srp-hap";
|
||||||
import { beforeAll, describe, expect, it } from "vitest";
|
import { beforeAll, describe, expect, it } from "vitest";
|
||||||
import { ApiClient } from "../../src/api/client.js";
|
import { ApiClient, ApiError } from "../../src/api/client.js";
|
||||||
import {
|
import {
|
||||||
init,
|
init,
|
||||||
toBase64,
|
toBase64,
|
||||||
@@ -142,10 +142,15 @@ const buildServerFixture = async (password: string) => {
|
|||||||
*
|
*
|
||||||
* @param fixture The server fixture from buildServerFixture
|
* @param fixture The server fixture from buildServerFixture
|
||||||
* @param opts.requireTOTP If true, verify-session returns a 2FA challenge
|
* @param opts.requireTOTP If true, verify-session returns a 2FA challenge
|
||||||
|
* @param opts.unknownEmail If true, the server has no account with the email
|
||||||
*/
|
*/
|
||||||
const buildMockFetch = (
|
const buildMockFetch = (
|
||||||
fixture: Awaited<ReturnType<typeof buildServerFixture>>,
|
fixture: Awaited<ReturnType<typeof buildServerFixture>>,
|
||||||
opts?: { requireTOTP?: boolean; requirePasskeyAndTOTP?: boolean },
|
opts?: {
|
||||||
|
requireTOTP?: boolean;
|
||||||
|
requirePasskeyAndTOTP?: boolean;
|
||||||
|
unknownEmail?: boolean;
|
||||||
|
},
|
||||||
) => {
|
) => {
|
||||||
let srpServer: SrpServer;
|
let srpServer: SrpServer;
|
||||||
const sessionID = "test-session-id";
|
const sessionID = "test-session-id";
|
||||||
@@ -163,7 +168,15 @@ const buildMockFetch = (
|
|||||||
const parsed = new URL(url);
|
const parsed = new URL(url);
|
||||||
const path = parsed.pathname;
|
const path = parsed.pathname;
|
||||||
|
|
||||||
|
// The Ente server answers its refusals with an empty JSON object.
|
||||||
|
const refusal = (status: number) =>
|
||||||
|
new Response("{}", {
|
||||||
|
status,
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
});
|
||||||
|
|
||||||
if (path === "/users/srp/attributes") {
|
if (path === "/users/srp/attributes") {
|
||||||
|
if (opts?.unknownEmail) return refusal(404);
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({ attributes: fixture.srpAttributes }),
|
JSON.stringify({ attributes: fixture.srpAttributes }),
|
||||||
{
|
{
|
||||||
@@ -197,7 +210,11 @@ const buildMockFetch = (
|
|||||||
|
|
||||||
if (path === "/users/srp/verify-session") {
|
if (path === "/users/srp/verify-session") {
|
||||||
const body = JSON.parse(init?.body as string);
|
const body = JSON.parse(init?.body as string);
|
||||||
|
try {
|
||||||
srpServer.checkM1(Buffer.from(body.srpM1, "base64"));
|
srpServer.checkM1(Buffer.from(body.srpM1, "base64"));
|
||||||
|
} catch {
|
||||||
|
return refusal(401);
|
||||||
|
}
|
||||||
const M2 = srpServer.computeM2();
|
const M2 = srpServer.computeM2();
|
||||||
|
|
||||||
// IMPORTANT: the museum server's EmailAuthorizationResponse
|
// IMPORTANT: the museum server's EmailAuthorizationResponse
|
||||||
@@ -365,13 +382,70 @@ describe("beginLogin via SRP", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("rejects a wrong password (SRP M1 verification fails)", async () => {
|
it("says a wrong password is incorrect (SRP M1 verification fails)", async () => {
|
||||||
const fixture = await buildServerFixture(TEST_PASSWORD);
|
const fixture = await buildServerFixture(TEST_PASSWORD);
|
||||||
const api = new ApiClient({ fetch: buildMockFetch(fixture) });
|
const api = new ApiClient({ fetch: buildMockFetch(fixture) });
|
||||||
|
|
||||||
|
const err = await beginLogin(api, TEST_EMAIL, "wrong password").catch(
|
||||||
|
(e: unknown) => e,
|
||||||
|
);
|
||||||
|
expect(err).toBeInstanceOf(ApiError);
|
||||||
|
expect((err as ApiError).message).toBe("incorrect email or password");
|
||||||
|
expect((err as ApiError).status).toBe(401);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("says the server has no account with an unknown email", async () => {
|
||||||
|
const fixture = await buildServerFixture(TEST_PASSWORD);
|
||||||
|
const api = new ApiClient({
|
||||||
|
fetch: buildMockFetch(fixture, { unknownEmail: true }),
|
||||||
|
});
|
||||||
|
|
||||||
|
const err = await beginLogin(api, TEST_EMAIL, TEST_PASSWORD).catch(
|
||||||
|
(e: unknown) => e,
|
||||||
|
);
|
||||||
|
expect(err).toBeInstanceOf(ApiError);
|
||||||
|
expect((err as ApiError).message).toBe(
|
||||||
|
"no account with this email on the server",
|
||||||
|
);
|
||||||
|
expect((err as ApiError).status).toBe(404);
|
||||||
|
});
|
||||||
|
|
||||||
|
// The Ente server's answer for an account that has no SRP attributes.
|
||||||
|
it("does not call an account with no SRP attributes missing", async () => {
|
||||||
|
const api = new ApiClient({
|
||||||
|
fetch: (async () =>
|
||||||
|
new Response(
|
||||||
|
JSON.stringify({ code: "NOT_FOUND", message: "" }),
|
||||||
|
{
|
||||||
|
status: 404,
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
},
|
||||||
|
)) as typeof globalThis.fetch,
|
||||||
|
});
|
||||||
|
|
||||||
|
const err = await beginLogin(api, TEST_EMAIL, TEST_PASSWORD).catch(
|
||||||
|
(e: unknown) => e,
|
||||||
|
);
|
||||||
|
expect(err).toBeInstanceOf(ApiError);
|
||||||
|
expect((err as ApiError).code).toBe("NOT_FOUND");
|
||||||
|
expect((err as ApiError).message).not.toBe(
|
||||||
|
"no account with this email on the server",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// A server address that points somewhere other than an Ente server.
|
||||||
|
it("keeps the bare status of a 404 that is not JSON", async () => {
|
||||||
|
const api = new ApiClient({
|
||||||
|
fetch: (async () =>
|
||||||
|
new Response("404 page not found", {
|
||||||
|
status: 404,
|
||||||
|
headers: { "content-type": "text/plain" },
|
||||||
|
})) as typeof globalThis.fetch,
|
||||||
|
});
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
beginLogin(api, TEST_EMAIL, "wrong password"),
|
beginLogin(api, TEST_EMAIL, TEST_PASSWORD),
|
||||||
).rejects.toThrow();
|
).rejects.toThrow(/^HTTP 404$/);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -77,17 +77,8 @@ const PASSWORD = "backup-password";
|
|||||||
const USER_ID = 42;
|
const USER_ID = 42;
|
||||||
const FRIEND_ID = 99;
|
const FRIEND_ID = 99;
|
||||||
|
|
||||||
// quak prints the time a backup starts, and dates save paths, in local time.
|
// Noon on 1 March 2024 in the time zone of the machine running the test, the
|
||||||
// The zone is pinned here, before any fixture is dated, and restored after
|
// one quak dates save paths in, in microseconds, as Ente records times.
|
||||||
// the last test.
|
|
||||||
const savedTZ = process.env.TZ;
|
|
||||||
process.env.TZ = "Europe/Berlin";
|
|
||||||
// The time every backup's clock reads, 03:30 in Berlin.
|
|
||||||
const NOW = new Date(Date.UTC(2026, 10, 11, 2, 30));
|
|
||||||
const STARTED = "Started at 2026-11-11 03:30:00 +0100\n";
|
|
||||||
|
|
||||||
// Noon on 1 March 2024 in local time, the one quak dates save paths in, in
|
|
||||||
// microseconds, as Ente records times.
|
|
||||||
const TAKEN = new Date(2024, 2, 1, 12).getTime() * 1000;
|
const TAKEN = new Date(2024, 2, 1, 12).getTime() * 1000;
|
||||||
// The date folder every original is saved in.
|
// The date folder every original is saved in.
|
||||||
const DAY = "2024/2024-03/2024-03-01";
|
const DAY = "2024/2024-03/2024-03-01";
|
||||||
@@ -287,8 +278,6 @@ beforeAll(async () => {
|
|||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
rmSync(root, { recursive: true, force: true });
|
rmSync(root, { recursive: true, force: true });
|
||||||
if (savedTZ === undefined) delete process.env.TZ;
|
|
||||||
else process.env.TZ = savedTZ;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Run `quak backup <dir>` as `bin/quak.ts` runs it, sending its requests
|
// Run `quak backup <dir>` as `bin/quak.ts` runs it, sending its requests
|
||||||
@@ -337,7 +326,6 @@ const backup = async (
|
|||||||
login: prompted,
|
login: prompted,
|
||||||
prompt: prompted,
|
prompt: prompted,
|
||||||
promptSecret: prompted,
|
promptSecret: prompted,
|
||||||
now: () => NOW,
|
|
||||||
};
|
};
|
||||||
const code = await new Promise<number>((resolve) => {
|
const code = await new Promise<number>((resolve) => {
|
||||||
void run(backupCommand(ctx, dir, opts), stdout, stderr, log, resolve);
|
void run(backupCommand(ctx, dir, opts), stdout, stderr, log, resolve);
|
||||||
@@ -710,7 +698,6 @@ describe("quak backup", () => {
|
|||||||
expect(code).toBe(3);
|
expect(code).toBe(3);
|
||||||
expect(stderr).toBe(
|
expect(stderr).toBe(
|
||||||
"Starting backup...\n" +
|
"Starting backup...\n" +
|
||||||
STARTED +
|
|
||||||
"Refreshing library...\n" +
|
"Refreshing library...\n" +
|
||||||
`quak: the saved session is no longer valid; run "quak login"\n`,
|
`quak: the saved session is no longer valid; run "quak login"\n`,
|
||||||
);
|
);
|
||||||
@@ -738,7 +725,6 @@ describe("quak backup", () => {
|
|||||||
expect(code).toBe(1);
|
expect(code).toBe(1);
|
||||||
expect(stderr).toBe(
|
expect(stderr).toBe(
|
||||||
"Starting backup...\n" +
|
"Starting backup...\n" +
|
||||||
STARTED +
|
|
||||||
"Refreshing library...\n" +
|
"Refreshing library...\n" +
|
||||||
`quak: ${reason}\n`,
|
`quak: ${reason}\n`,
|
||||||
);
|
);
|
||||||
@@ -1131,7 +1117,6 @@ describe("a backup whose lock is lost", () => {
|
|||||||
expect(code).toBe(1);
|
expect(code).toBe(1);
|
||||||
expect(stderr).toBe(
|
expect(stderr).toBe(
|
||||||
"Starting backup...\n" +
|
"Starting backup...\n" +
|
||||||
STARTED +
|
|
||||||
"Fetched the files of 1 of 3 collections\n" +
|
"Fetched the files of 1 of 3 collections\n" +
|
||||||
"Fetched the files of 2 of 3 collections\n" +
|
"Fetched the files of 2 of 3 collections\n" +
|
||||||
"Fetched the files of 3 of 3 collections\n" +
|
"Fetched the files of 3 of 3 collections\n" +
|
||||||
|
|||||||
@@ -319,7 +319,6 @@ const context = (ente: FakeEnte): CliContext => {
|
|||||||
promptSecret: async () => {
|
promptSecret: async () => {
|
||||||
throw new Error("prompt not expected");
|
throw new Error("prompt not expected");
|
||||||
},
|
},
|
||||||
now: () => new Date(Date.UTC(2026, 10, 11, 2, 30)),
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ import {
|
|||||||
vi,
|
vi,
|
||||||
beforeAll,
|
beforeAll,
|
||||||
beforeEach,
|
beforeEach,
|
||||||
afterAll,
|
|
||||||
afterEach,
|
afterEach,
|
||||||
} from "vitest";
|
} from "vitest";
|
||||||
|
|
||||||
@@ -195,12 +194,6 @@ const FETCHED_BOTH_ALBUMS =
|
|||||||
"Fetched the files of 1 of 2 collections\n" +
|
"Fetched the files of 1 of 2 collections\n" +
|
||||||
"Fetched the files of 2 of 2 collections\n";
|
"Fetched the files of 2 of 2 collections\n";
|
||||||
|
|
||||||
// The time every context's clock reads. `backup` prints it in local time, and
|
|
||||||
// the tests pin the zone to Europe/Berlin, where it is 03:30.
|
|
||||||
const NOW = new Date(Date.UTC(2026, 10, 11, 2, 30));
|
|
||||||
const STARTED = "Started at 2026-11-11 03:30:00 +0100\n";
|
|
||||||
const FINISHED = "Finished at 2026-11-11 03:30:00 +0100\n";
|
|
||||||
|
|
||||||
// Collects everything written to it.
|
// Collects everything written to it.
|
||||||
class Output {
|
class Output {
|
||||||
text = "";
|
text = "";
|
||||||
@@ -230,18 +223,11 @@ const context = (client: Client | null = fakeClient()): CliContext => ({
|
|||||||
promptSecret: async () => {
|
promptSecret: async () => {
|
||||||
throw new Error("prompt not expected");
|
throw new Error("prompt not expected");
|
||||||
},
|
},
|
||||||
now: () => NOW,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const savedTZ = process.env.TZ;
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
process.env.TZ = "Europe/Berlin";
|
|
||||||
await init();
|
await init();
|
||||||
});
|
});
|
||||||
afterAll(() => {
|
|
||||||
if (savedTZ === undefined) delete process.env.TZ;
|
|
||||||
else process.env.TZ = savedTZ;
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
root = mkdtempSync(join(tmpdir(), "quak-cli-test-"));
|
root = mkdtempSync(join(tmpdir(), "quak-cli-test-"));
|
||||||
@@ -973,7 +959,6 @@ describe("backup", () => {
|
|||||||
expect(await backupCommand(context(), dir, {})).toBe(0);
|
expect(await backupCommand(context(), dir, {})).toBe(0);
|
||||||
expect(stderr.text).toContain(
|
expect(stderr.text).toContain(
|
||||||
"\n--- Backup complete ---\n" +
|
"\n--- Backup complete ---\n" +
|
||||||
FINISHED +
|
|
||||||
" Total files: 3\n" +
|
" Total files: 3\n" +
|
||||||
" Downloaded: 3\n" +
|
" Downloaded: 3\n" +
|
||||||
" Skipped: 0\n" +
|
" Skipped: 0\n" +
|
||||||
@@ -983,43 +968,6 @@ describe("backup", () => {
|
|||||||
expect(existsSync(join(dir, "backup.lock"))).toBe(false);
|
expect(existsSync(join(dir, "backup.lock"))).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
// A log of many nights' runs shows when each started and finished, in
|
|
||||||
// local time with the offset from UTC.
|
|
||||||
it("prints the local date and time the run started and finished", async () => {
|
|
||||||
const start = new Date(Date.UTC(2026, 10, 11, 2, 30, 5));
|
|
||||||
const end = new Date(Date.UTC(2026, 10, 11, 3, 4, 9));
|
|
||||||
const zones = [
|
|
||||||
{
|
|
||||||
zone: "Europe/Berlin",
|
|
||||||
started: "2026-11-11 03:30:05 +0100",
|
|
||||||
finished: "2026-11-11 04:04:09 +0100",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
zone: "Asia/Kolkata",
|
|
||||||
started: "2026-11-11 08:00:05 +0530",
|
|
||||||
finished: "2026-11-11 08:34:09 +0530",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
zone: "America/New_York",
|
|
||||||
started: "2026-11-10 21:30:05 -0500",
|
|
||||||
finished: "2026-11-10 22:04:09 -0500",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
for (const { zone, started, finished } of zones) {
|
|
||||||
process.env.TZ = zone;
|
|
||||||
stderr.text = "";
|
|
||||||
const times = [start, end];
|
|
||||||
const ctx = { ...context(), now: () => times.shift()! };
|
|
||||||
expect(await backupCommand(ctx, join(root, zone), {})).toBe(0);
|
|
||||||
const begin = `Starting backup...\nStarted at ${started}\n`;
|
|
||||||
expect(stderr.text.slice(0, begin.length)).toBe(begin);
|
|
||||||
expect(stderr.text).toContain(
|
|
||||||
`--- Backup complete ---\nFinished at ${finished}\n`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
process.env.TZ = "Europe/Berlin";
|
|
||||||
});
|
|
||||||
|
|
||||||
// The backup opens its library with the precache off: it fetches the
|
// The backup opens its library with the precache off: it fetches the
|
||||||
// originals it needs into the backup, and must not also fetch every
|
// originals it needs into the backup, and must not also fetch every
|
||||||
// thumbnail in the account, or keep originals, in the per-user cache.
|
// thumbnail in the account, or keep originals, in the per-user cache.
|
||||||
@@ -1101,7 +1049,6 @@ describe("backup", () => {
|
|||||||
it("counts on stderr each album whose files its first refresh fetched, with --json too", async () => {
|
it("counts on stderr each album whose files its first refresh fetched, with --json too", async () => {
|
||||||
const start =
|
const start =
|
||||||
"Starting backup...\n" +
|
"Starting backup...\n" +
|
||||||
STARTED +
|
|
||||||
FETCHED_BOTH_ALBUMS +
|
FETCHED_BOTH_ALBUMS +
|
||||||
"Refreshing library...\n";
|
"Refreshing library...\n";
|
||||||
for (const json of [false, true]) {
|
for (const json of [false, true]) {
|
||||||
@@ -1221,9 +1168,7 @@ describe("backup", () => {
|
|||||||
const { code, runText } = await backupThroughRun(context(client), dir);
|
const { code, runText } = await backupThroughRun(context(client), dir);
|
||||||
expect(code).toBe(1);
|
expect(code).toBe(1);
|
||||||
expect(runText).toBe("quak: HTTP 503 from server\n");
|
expect(runText).toBe("quak: HTTP 503 from server\n");
|
||||||
expect(stderr.text).toBe(
|
expect(stderr.text).toBe("Starting backup...\nRefreshing library...\n");
|
||||||
"Starting backup...\n" + STARTED + "Refreshing library...\n",
|
|
||||||
);
|
|
||||||
expect(readdirSync(dir)).toEqual([]);
|
expect(readdirSync(dir)).toEqual([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1254,9 +1199,7 @@ describe("backup", () => {
|
|||||||
expect(runText).toBe(
|
expect(runText).toBe(
|
||||||
`quak: the saved session is no longer valid; run "quak login"\n`,
|
`quak: the saved session is no longer valid; run "quak login"\n`,
|
||||||
);
|
);
|
||||||
expect(stderr.text).toBe(
|
expect(stderr.text).toBe("Starting backup...\nRefreshing library...\n");
|
||||||
"Starting backup...\n" + STARTED + "Refreshing library...\n",
|
|
||||||
);
|
|
||||||
expect(readdirSync(dir)).toEqual([]);
|
expect(readdirSync(dir)).toEqual([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1284,9 +1227,7 @@ describe("backup", () => {
|
|||||||
expect(await backupCommand(context(client), dir, {})).toBe(2);
|
expect(await backupCommand(context(client), dir, {})).toBe(2);
|
||||||
expect(requests).toBe(0);
|
expect(requests).toBe(0);
|
||||||
expect(stderr.text).toBe(
|
expect(stderr.text).toBe(
|
||||||
"Starting backup...\n" +
|
`Starting backup...\nquak: another backup of ${dir} is running\n`,
|
||||||
STARTED +
|
|
||||||
`quak: another backup of ${dir} is running\n`,
|
|
||||||
);
|
);
|
||||||
expect(readdirSync(dir)).toEqual(["backup.lock"]);
|
expect(readdirSync(dir)).toEqual(["backup.lock"]);
|
||||||
// Opening the library would have made its cache directory.
|
// Opening the library would have made its cache directory.
|
||||||
|
|||||||
Reference in new issue
Block a user