quak backup: print the date and time a run starts and finishes (closes #245)
check / check (push) Waiting to run
check / check (push) Waiting to run
Adds a `Started at` line after `Starting backup...` and a `Finished at` line after `--- Backup complete ---`, in local time with the UTC offset. No existing line changes. The clock comes in through `CliContext.now`. The README's crontab now pipes the output to `logger`, which adds its own time to each line; appending to a file remains as the alternative. Model: opus-5-5
This commit was merged in pull request #256.
This commit is contained in:
1 parent
2492946e9d
commit
ecdd90b0a8
6 files changed
+124
-19
No files matched your search
@@ -77,8 +77,17 @@ const PASSWORD = "backup-password";
|
||||
const USER_ID = 42;
|
||||
const FRIEND_ID = 99;
|
||||
|
||||
// Noon on 1 March 2024 in the time zone of the machine running the test, the
|
||||
// one quak dates save paths in, in microseconds, as Ente records times.
|
||||
// quak prints the time a backup starts, and dates save paths, in local time.
|
||||
// The zone is pinned here, before any fixture is dated, and restored after
|
||||
// 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;
|
||||
// The date folder every original is saved in.
|
||||
const DAY = "2024/2024-03/2024-03-01";
|
||||
@@ -278,6 +287,8 @@ beforeAll(async () => {
|
||||
|
||||
afterAll(() => {
|
||||
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
|
||||
@@ -326,6 +337,7 @@ const backup = async (
|
||||
login: prompted,
|
||||
prompt: prompted,
|
||||
promptSecret: prompted,
|
||||
now: () => NOW,
|
||||
};
|
||||
const code = await new Promise<number>((resolve) => {
|
||||
void run(backupCommand(ctx, dir, opts), stdout, stderr, log, resolve);
|
||||
@@ -698,6 +710,7 @@ describe("quak backup", () => {
|
||||
expect(code).toBe(3);
|
||||
expect(stderr).toBe(
|
||||
"Starting backup...\n" +
|
||||
STARTED +
|
||||
"Refreshing library...\n" +
|
||||
`quak: the saved session is no longer valid; run "quak login"\n`,
|
||||
);
|
||||
@@ -725,6 +738,7 @@ describe("quak backup", () => {
|
||||
expect(code).toBe(1);
|
||||
expect(stderr).toBe(
|
||||
"Starting backup...\n" +
|
||||
STARTED +
|
||||
"Refreshing library...\n" +
|
||||
`quak: ${reason}\n`,
|
||||
);
|
||||
@@ -1117,6 +1131,7 @@ describe("a backup whose lock is lost", () => {
|
||||
expect(code).toBe(1);
|
||||
expect(stderr).toBe(
|
||||
"Starting backup...\n" +
|
||||
STARTED +
|
||||
"Fetched the files of 1 of 3 collections\n" +
|
||||
"Fetched the files of 2 of 3 collections\n" +
|
||||
"Fetched the files of 3 of 3 collections\n" +
|
||||
|
||||
@@ -319,6 +319,7 @@ const context = (ente: FakeEnte): CliContext => {
|
||||
promptSecret: async () => {
|
||||
throw new Error("prompt not expected");
|
||||
},
|
||||
now: () => new Date(Date.UTC(2026, 10, 11, 2, 30)),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ import {
|
||||
vi,
|
||||
beforeAll,
|
||||
beforeEach,
|
||||
afterAll,
|
||||
afterEach,
|
||||
} from "vitest";
|
||||
|
||||
@@ -194,6 +195,12 @@ const FETCHED_BOTH_ALBUMS =
|
||||
"Fetched the files of 1 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.
|
||||
class Output {
|
||||
text = "";
|
||||
@@ -223,11 +230,18 @@ const context = (client: Client | null = fakeClient()): CliContext => ({
|
||||
promptSecret: async () => {
|
||||
throw new Error("prompt not expected");
|
||||
},
|
||||
now: () => NOW,
|
||||
});
|
||||
|
||||
const savedTZ = process.env.TZ;
|
||||
beforeAll(async () => {
|
||||
process.env.TZ = "Europe/Berlin";
|
||||
await init();
|
||||
});
|
||||
afterAll(() => {
|
||||
if (savedTZ === undefined) delete process.env.TZ;
|
||||
else process.env.TZ = savedTZ;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
root = mkdtempSync(join(tmpdir(), "quak-cli-test-"));
|
||||
@@ -959,6 +973,7 @@ describe("backup", () => {
|
||||
expect(await backupCommand(context(), dir, {})).toBe(0);
|
||||
expect(stderr.text).toContain(
|
||||
"\n--- Backup complete ---\n" +
|
||||
FINISHED +
|
||||
" Total files: 3\n" +
|
||||
" Downloaded: 3\n" +
|
||||
" Skipped: 0\n" +
|
||||
@@ -968,6 +983,43 @@ describe("backup", () => {
|
||||
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
|
||||
// originals it needs into the backup, and must not also fetch every
|
||||
// thumbnail in the account, or keep originals, in the per-user cache.
|
||||
@@ -1049,6 +1101,7 @@ describe("backup", () => {
|
||||
it("counts on stderr each album whose files its first refresh fetched, with --json too", async () => {
|
||||
const start =
|
||||
"Starting backup...\n" +
|
||||
STARTED +
|
||||
FETCHED_BOTH_ALBUMS +
|
||||
"Refreshing library...\n";
|
||||
for (const json of [false, true]) {
|
||||
@@ -1168,7 +1221,9 @@ describe("backup", () => {
|
||||
const { code, runText } = await backupThroughRun(context(client), dir);
|
||||
expect(code).toBe(1);
|
||||
expect(runText).toBe("quak: HTTP 503 from server\n");
|
||||
expect(stderr.text).toBe("Starting backup...\nRefreshing library...\n");
|
||||
expect(stderr.text).toBe(
|
||||
"Starting backup...\n" + STARTED + "Refreshing library...\n",
|
||||
);
|
||||
expect(readdirSync(dir)).toEqual([]);
|
||||
});
|
||||
|
||||
@@ -1199,7 +1254,9 @@ describe("backup", () => {
|
||||
expect(runText).toBe(
|
||||
`quak: the saved session is no longer valid; run "quak login"\n`,
|
||||
);
|
||||
expect(stderr.text).toBe("Starting backup...\nRefreshing library...\n");
|
||||
expect(stderr.text).toBe(
|
||||
"Starting backup...\n" + STARTED + "Refreshing library...\n",
|
||||
);
|
||||
expect(readdirSync(dir)).toEqual([]);
|
||||
});
|
||||
|
||||
@@ -1227,7 +1284,9 @@ describe("backup", () => {
|
||||
expect(await backupCommand(context(client), dir, {})).toBe(2);
|
||||
expect(requests).toBe(0);
|
||||
expect(stderr.text).toBe(
|
||||
`Starting backup...\nquak: another backup of ${dir} is running\n`,
|
||||
"Starting backup...\n" +
|
||||
STARTED +
|
||||
`quak: another backup of ${dir} is running\n`,
|
||||
);
|
||||
expect(readdirSync(dir)).toEqual(["backup.lock"]);
|
||||
// Opening the library would have made its cache directory.
|
||||
|
||||
Reference in new issue
Block a user