cursedbelt-server 4.48.1 → 4.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server/d1/pullD1.d.ts +4 -1
- package/dist/server/d1/pullD1.js +5 -2
- package/dist/server/dropzone/index.d.ts +1 -0
- package/dist/server/dropzone/index.js +1 -0
- package/dist/server/dropzone/report.d.ts +188 -0
- package/dist/server/dropzone/report.js +430 -0
- package/dist/server/dropzone/scan.d.ts +21 -2
- package/dist/server/dropzone/scan.js +46 -1
- package/package.json +1 -1
- package/src/server/d1/pullD1.spec.ts +8 -0
- package/src/server/d1/pullD1.ts +5 -2
- package/src/server/dropzone/index.ts +8 -0
- package/src/server/dropzone/report.spec.ts +275 -0
- package/src/server/dropzone/report.ts +568 -0
- package/src/server/dropzone/scan.ts +67 -4
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
* should not stall uploading the rest of the in progress uploads"* — and it generalizes: a
|
|
32
32
|
* dropzone with one unreadable file in it must still drain the other 4,000.
|
|
33
33
|
*/
|
|
34
|
+
import type { IngestReporter, ReportResult } from "./report.js";
|
|
34
35
|
/** A file the sweep found, with everything a handler needs to decide about it. */
|
|
35
36
|
export interface DroppedFile {
|
|
36
37
|
/** Absolute path on disk. */
|
|
@@ -149,8 +150,20 @@ export declare function isStable(file: DroppedFile, stabilityMs?: number): Promi
|
|
|
149
150
|
* files that must survive.
|
|
150
151
|
*/
|
|
151
152
|
export declare function pruneEmptyDirs(root: string, dir?: string): Promise<number>;
|
|
152
|
-
/**
|
|
153
|
-
|
|
153
|
+
/**
|
|
154
|
+
* What the app supplies: what to do with one file. `uploading(key)` names the binary-server key its
|
|
155
|
+
* bytes stage under, once the handler knows it — station's Uploads page shows that transfer's own
|
|
156
|
+
* progress on the file's row.
|
|
157
|
+
*/
|
|
158
|
+
export type DropHandler = (file: DroppedFile, ctx: {
|
|
159
|
+
uploading(key: string): void;
|
|
160
|
+
}) => Promise<DropOutcome>;
|
|
161
|
+
/**
|
|
162
|
+
* A sweep's outcome in the words station's Uploads page reads (`report.ts`) — `null` for `held`, which
|
|
163
|
+
* is a standing state rather than an event: the file stays where it is and every sweep meets it
|
|
164
|
+
* again, so it is shown as left alone (`skipped`) instead of journaled every five minutes.
|
|
165
|
+
*/
|
|
166
|
+
export declare function reportResultOf(outcome: DropOutcome): ReportResult | null;
|
|
154
167
|
/**
|
|
155
168
|
* Walk a dropzone, hand every file to `handle`, and leave nothing behind that was dealt with.
|
|
156
169
|
*
|
|
@@ -161,6 +174,12 @@ export type DropHandler = (file: DroppedFile) => Promise<DropOutcome>;
|
|
|
161
174
|
export declare function sweepDropzone(root: string, handle: DropHandler, opts?: ScanOptions & {
|
|
162
175
|
dryRun?: boolean;
|
|
163
176
|
onProgress?: (done: number, total: number) => void;
|
|
177
|
+
/**
|
|
178
|
+
* Write what the sweep is doing for station's Uploads page (`createIngestReporter`). The dropzone
|
|
179
|
+
* is ONE account, id `""`: each top-level folder is a source, loose files one more. Never on a
|
|
180
|
+
* dry run, which files nothing. Every hook is guarded — a reporter fault never costs a file.
|
|
181
|
+
*/
|
|
182
|
+
report?: IngestReporter;
|
|
164
183
|
}): Promise<SweepReport>;
|
|
165
184
|
/** A one-line summary — every consumer prints one, and they should read alike. */
|
|
166
185
|
export declare const describeSweep: (report: SweepReport) => string;
|
|
@@ -185,6 +185,27 @@ export async function pruneEmptyDirs(root, dir = root) {
|
|
|
185
185
|
return removed; // not empty — the right outcome, and not an error
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* A sweep's outcome in the words station's Uploads page reads (`report.ts`) — `null` for `held`, which
|
|
190
|
+
* is a standing state rather than an event: the file stays where it is and every sweep meets it
|
|
191
|
+
* again, so it is shown as left alone (`skipped`) instead of journaled every five minutes.
|
|
192
|
+
*/
|
|
193
|
+
export function reportResultOf(outcome) {
|
|
194
|
+
const detail = outcome.detail ? { detail: outcome.detail } : {};
|
|
195
|
+
switch (outcome.kind) {
|
|
196
|
+
case "stored":
|
|
197
|
+
return { outcome: "added", ...detail };
|
|
198
|
+
case "duplicate":
|
|
199
|
+
return { outcome: "duplicate", ...detail };
|
|
200
|
+
// Refused by the app's own rule (a format it does not take) — not filed, and not a failure.
|
|
201
|
+
case "discard":
|
|
202
|
+
return { outcome: "blocked", detail: outcome.detail ?? "not something this app files" };
|
|
203
|
+
case "held":
|
|
204
|
+
return null;
|
|
205
|
+
case "failed":
|
|
206
|
+
return { outcome: "failed", detail: outcome.detail };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
188
209
|
/**
|
|
189
210
|
* Walk a dropzone, hand every file to `handle`, and leave nothing behind that was dealt with.
|
|
190
211
|
*
|
|
@@ -193,6 +214,19 @@ export async function pruneEmptyDirs(root, dir = root) {
|
|
|
193
214
|
* silently. A handler says what happened; this decides what that means for the file on disk.
|
|
194
215
|
*/
|
|
195
216
|
export async function sweepDropzone(root, handle, opts = {}) {
|
|
217
|
+
const reporter = opts.dryRun ? undefined : opts.report;
|
|
218
|
+
try {
|
|
219
|
+
const report = await sweep(root, handle, opts, reporter);
|
|
220
|
+
reporter?.finish();
|
|
221
|
+
return report;
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
reporter?.finish(error instanceof Error ? error.message : String(error));
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
async function sweep(root, handle, opts, reporter) {
|
|
229
|
+
const as = (file) => ({ accountId: "", path: file.path, relativePath: file.relativePath, size: file.size });
|
|
196
230
|
const report = {
|
|
197
231
|
scanned: 0,
|
|
198
232
|
stored: 0,
|
|
@@ -205,17 +239,23 @@ export async function sweepDropzone(root, handle, opts = {}) {
|
|
|
205
239
|
emptyDirsRemoved: 0,
|
|
206
240
|
remaining: 0,
|
|
207
241
|
};
|
|
242
|
+
reporter?.walking();
|
|
208
243
|
const files = await scanDropzone(root, opts);
|
|
244
|
+
reporter?.found(files.map(as));
|
|
209
245
|
for (const [index, file] of files.entries()) {
|
|
210
246
|
report.scanned++;
|
|
211
247
|
opts.onProgress?.(index + 1, files.length);
|
|
212
248
|
if (!(await isStable(file, opts.stabilityMs))) {
|
|
213
249
|
report.unstable.push(file.relativePath);
|
|
250
|
+
reporter?.progress.waiting(as(file), "copying");
|
|
214
251
|
continue;
|
|
215
252
|
}
|
|
216
253
|
let outcome;
|
|
254
|
+
reporter?.progress.uploading(as(file), "");
|
|
217
255
|
try {
|
|
218
|
-
outcome = opts.dryRun
|
|
256
|
+
outcome = opts.dryRun
|
|
257
|
+
? { kind: "held", detail: "dry run" }
|
|
258
|
+
: await handle(file, { uploading: (key) => reporter?.progress.uploading(as(file), key) });
|
|
219
259
|
}
|
|
220
260
|
catch (error) {
|
|
221
261
|
// Rule 3: a handler that throws costs one file, never the sweep.
|
|
@@ -224,6 +264,11 @@ export async function sweepDropzone(root, handle, opts = {}) {
|
|
|
224
264
|
detail: error instanceof Error ? error.message : String(error),
|
|
225
265
|
};
|
|
226
266
|
}
|
|
267
|
+
const result = reportResultOf(outcome);
|
|
268
|
+
if (result)
|
|
269
|
+
reporter?.result(as(file), result);
|
|
270
|
+
else
|
|
271
|
+
reporter?.progress.waiting(as(file), "skipped");
|
|
227
272
|
switch (outcome.kind) {
|
|
228
273
|
case "stored":
|
|
229
274
|
case "duplicate":
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cursedbelt-server",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.49.0",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "The app-facing Bun/Hono server tier of the cursedbelt split — storage, sharing, activity, guard, sync. React-free; cursedbelt-core below it.",
|
|
@@ -336,6 +336,14 @@ describe('pulling D1 down — paged SELECTs, table by table, never an export', (
|
|
|
336
336
|
expect(slept).toEqual([WAITS[0] as number]);
|
|
337
337
|
});
|
|
338
338
|
|
|
339
|
+
test("🔴 wrangler crashing on `setTypeOfService EINVAL` is a dropped connection, waited out (vault, 2026-10-23)", async () => {
|
|
340
|
+
const source = live("INSERT INTO items VALUES ('i1', 'a');");
|
|
341
|
+
const said = 'error: could not list D1\'s tables: …\nerror: setTypeOfService EINVAL\n errno: -22,\n syscall: "setTypeOfService",\n code: "EINVAL"';
|
|
342
|
+
const slept: number[] = [];
|
|
343
|
+
await pullWaiting(scratch(), blipping(source, (sql) => sql.includes('sqlite_master'), 1, said).run, slept);
|
|
344
|
+
expect(slept).toEqual([WAITS[0] as number]);
|
|
345
|
+
});
|
|
346
|
+
|
|
339
347
|
test('a wire that stays down still REFUSES, with its own words, after every wait', async () => {
|
|
340
348
|
const source = live("INSERT INTO items VALUES ('i1', 'a');");
|
|
341
349
|
const slept: number[] = [];
|
package/src/server/d1/pullD1.ts
CHANGED
|
@@ -244,12 +244,15 @@ export const TRANSIENT_WAITS_MS: readonly number[] = [5_000, 20_000, 60_000];
|
|
|
244
244
|
|
|
245
245
|
/**
|
|
246
246
|
* A failure of the WIRE between this Mac and Cloudflare, not an answer from D1 — which asking
|
|
247
|
-
* again a little later fixes.
|
|
247
|
+
* again a little later fixes. `setTypeOfService EINVAL` is one in disguise: undici sets the socket's
|
|
248
|
+
* TOS before every request, and macOS answers `setsockopt` on a connection the peer already reset
|
|
249
|
+
* with EINVAL — so under Bun a dropped connection crashes wrangler with it instead of ECONNRESET
|
|
250
|
+
* (vault's and auth's nightly pulls, 2026-10-23, both under the background clamp's starved handshakes). Only these are waited out — a refusal D1 actually made (a page too
|
|
248
251
|
* large, bad SQL, auth) is never retried here, so a page refused for its size still halves at
|
|
249
252
|
* once instead of sleeping ~1.5 minutes per halving.
|
|
250
253
|
*/
|
|
251
254
|
export const isTransient = (message: string): boolean =>
|
|
252
|
-
/fetch failed|ECONNRESET|ECONNREFUSED|ETIMEDOUT|ENOTFOUND|EAI_AGAIN|socket hang up|network connection was lost/i.test(
|
|
255
|
+
/fetch failed|ECONNRESET|ECONNREFUSED|ETIMEDOUT|ENOTFOUND|EAI_AGAIN|socket hang up|network connection was lost|setTypeOfService EINVAL/i.test(
|
|
253
256
|
message,
|
|
254
257
|
);
|
|
255
258
|
|
|
@@ -28,3 +28,11 @@ export {
|
|
|
28
28
|
type DuplicateVerdict,
|
|
29
29
|
type DuplicateQuestion,
|
|
30
30
|
} from "./duplicates.js";
|
|
31
|
+
export {
|
|
32
|
+
createIngestReporter,
|
|
33
|
+
FILES_JOURNAL,
|
|
34
|
+
type FileOutcome,
|
|
35
|
+
type IngestReporter,
|
|
36
|
+
type ReportFile,
|
|
37
|
+
type SourceDrive,
|
|
38
|
+
} from "./report.js";
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
createIngestReporter,
|
|
7
|
+
FILES_JOURNAL,
|
|
8
|
+
LOOSE_FILES,
|
|
9
|
+
PROGRESS_FILE,
|
|
10
|
+
QUEUE_FILE,
|
|
11
|
+
sourceIdOf,
|
|
12
|
+
type FileLine,
|
|
13
|
+
type IngestProgress,
|
|
14
|
+
type QueueLine,
|
|
15
|
+
type ReportFile,
|
|
16
|
+
type ReporterOptions,
|
|
17
|
+
} from "./report.js";
|
|
18
|
+
import { sweepDropzone } from "./scan.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* What a sweep writes for station's Uploads page (`report.ts`, forge task 3588). Each test plays one
|
|
22
|
+
* or two sweeps through the reporter's hooks the way a sweep calls them — collections' own, or
|
|
23
|
+
* `sweepDropzone`'s, which family and music run.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
let root: string;
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
root = mkdtempSync(join(tmpdir(), "dropzone-report-spec-"));
|
|
29
|
+
});
|
|
30
|
+
afterEach(() => rmSync(root, { recursive: true, force: true }));
|
|
31
|
+
|
|
32
|
+
const ACCOUNT = "acct-1";
|
|
33
|
+
|
|
34
|
+
function world() {
|
|
35
|
+
const stateDir = join(root, "state");
|
|
36
|
+
const accountDir = join(root, "dropzone", "2026-09-10 Account 1");
|
|
37
|
+
mkdirSync(accountDir, { recursive: true });
|
|
38
|
+
let clock = Date.parse("2026-10-22T12:00:00Z");
|
|
39
|
+
const reporter = (extra: Partial<ReporterOptions> = {}) =>
|
|
40
|
+
createIngestReporter({
|
|
41
|
+
stateDir,
|
|
42
|
+
app: "collections",
|
|
43
|
+
job: "com.collections.ingest",
|
|
44
|
+
everySeconds: 300,
|
|
45
|
+
dropzone: join(root, "dropzone"),
|
|
46
|
+
accounts: [{ id: ACCOUNT, label: "Account 1", dir: accountDir }],
|
|
47
|
+
now: () => clock,
|
|
48
|
+
pid: 1,
|
|
49
|
+
...extra,
|
|
50
|
+
});
|
|
51
|
+
const file = (relativePath: string, size: number): ReportFile => ({ path: join(accountDir, relativePath), relativePath, size, accountId: ACCOUNT });
|
|
52
|
+
const progress = (): IngestProgress => JSON.parse(readFileSync(join(stateDir, PROGRESS_FILE), "utf8")) as IngestProgress;
|
|
53
|
+
const lines = <T>(name: string): T[] =>
|
|
54
|
+
readFileSync(join(stateDir, name), "utf8")
|
|
55
|
+
.split("\n")
|
|
56
|
+
.filter(Boolean)
|
|
57
|
+
.map((l) => JSON.parse(l) as T);
|
|
58
|
+
return { stateDir, accountDir, reporter, file, progress, lines, tick: (ms: number) => (clock += ms) };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe("sources", () => {
|
|
62
|
+
test("each top-level folder or link is one source, and loose files are one more", () => {
|
|
63
|
+
expect(sourceIdOf(ACCOUNT, "tod/a/b.mp4")).toEqual({ id: `${ACCOUNT}:tod`, label: "tod", top: "tod" });
|
|
64
|
+
expect(sourceIdOf(ACCOUNT, "photo.jpg")).toEqual({ id: `${ACCOUNT}:`, label: LOOSE_FILES, top: null });
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("a linked folder over one sweep", () => {
|
|
69
|
+
test("settled, uploading, filed and waiting files each land in their tally, the queue and the journal", () => {
|
|
70
|
+
const w = world();
|
|
71
|
+
symlinkSync("/Volumes/databundle/tod", join(w.accountDir, "tod"));
|
|
72
|
+
const settled = w.file("tod/old.mp4", 100);
|
|
73
|
+
const going = w.file("tod/new.mp4", 200);
|
|
74
|
+
const waiting = w.file("tod/big.mp4", 5_000);
|
|
75
|
+
|
|
76
|
+
const r = w.reporter({ driveOf: (target) => ({ volume: "/Volumes/databundle", state: target.startsWith("/Volumes/databundle") ? "ok" : "deleted" }) });
|
|
77
|
+
r.walking();
|
|
78
|
+
r.found([settled, going, waiting], (f) => (f === settled ? { outcome: "added", at: "2026-10-21T09:00:00.000Z" } : undefined));
|
|
79
|
+
r.progress.waiting(settled, "done");
|
|
80
|
+
r.progress.waiting(waiting, "room");
|
|
81
|
+
r.progress.uploading(going, "file/u_abc");
|
|
82
|
+
|
|
83
|
+
const mid = w.progress().sources[0];
|
|
84
|
+
expect(mid).toMatchObject({ label: "tod", kind: "linked", target: "/Volumes/databundle/tod", drive: { volume: "/Volumes/databundle", state: "ok" }, startedAt: "2026-10-21T09:00:00.000Z", finishedAt: null });
|
|
85
|
+
expect(mid?.current).toMatchObject({ path: "tod/new.mp4", key: "file/u_abc", bytes: 200 });
|
|
86
|
+
expect(mid?.total).toEqual({ files: 3, bytes: 5_300 });
|
|
87
|
+
expect(mid?.done).toEqual({ files: 1, bytes: 100 });
|
|
88
|
+
|
|
89
|
+
w.tick(60_000);
|
|
90
|
+
r.result(going, { outcome: "added", itemId: "item-1" });
|
|
91
|
+
r.finish();
|
|
92
|
+
|
|
93
|
+
const end = w.progress();
|
|
94
|
+
expect(end.sweep).toMatchObject({ state: "finished", phase: "done" });
|
|
95
|
+
expect(end.sources[0]).toMatchObject({ added: 1, current: null, finishedAt: null, done: { files: 2, bytes: 300 }, waitingForRoom: { files: 1, bytes: 5_000 } });
|
|
96
|
+
expect(w.lines<QueueLine>(QUEUE_FILE)).toEqual([{ source: `${ACCOUNT}:tod`, path: "tod/big.mp4", bytes: 5_000, state: "waiting-room" }]);
|
|
97
|
+
// The settled file is the journal's first line, at its own date; the sweep's follows.
|
|
98
|
+
expect(w.lines<FileLine>(FILES_JOURNAL).map((l) => [l.path, l.outcome, Boolean(l.backfilled)])).toEqual([
|
|
99
|
+
["tod/old.mp4", "added", true],
|
|
100
|
+
["tod/new.mp4", "added", false],
|
|
101
|
+
]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("a drive that is not plugged in keeps the source's last totals, all waiting for the drive", () => {
|
|
105
|
+
const w = world();
|
|
106
|
+
symlinkSync("/Volumes/databundle/tod", join(w.accountDir, "tod"));
|
|
107
|
+
const a = w.file("tod/a.mp4", 100);
|
|
108
|
+
const b = w.file("tod/b.mp4", 300);
|
|
109
|
+
const first = w.reporter();
|
|
110
|
+
first.found([a, b]);
|
|
111
|
+
first.result(a, { outcome: "added", itemId: "i" });
|
|
112
|
+
first.finish();
|
|
113
|
+
|
|
114
|
+
const second = w.reporter({ driveOf: () => ({ volume: "/Volumes/databundle", state: "unmounted" }) });
|
|
115
|
+
second.walking();
|
|
116
|
+
second.unreachable(ACCOUNT, "tod", "/Volumes/databundle/tod");
|
|
117
|
+
second.found([]);
|
|
118
|
+
second.finish();
|
|
119
|
+
|
|
120
|
+
expect(w.progress().sources.find((s) => s.label === "tod")).toMatchObject({
|
|
121
|
+
total: { files: 2, bytes: 400 },
|
|
122
|
+
done: { files: 1, bytes: 100 },
|
|
123
|
+
waitingForDrive: { files: 1, bytes: 300 },
|
|
124
|
+
drive: { state: "unmounted" },
|
|
125
|
+
finishedAt: null,
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("🔴 a driveOf or room that throws costs the page a field, never the sweep a file", () => {
|
|
130
|
+
const w = world();
|
|
131
|
+
symlinkSync("/Volumes/databundle/tod", join(w.accountDir, "tod"));
|
|
132
|
+
const warned: string[] = [];
|
|
133
|
+
const r = w.reporter({
|
|
134
|
+
driveOf: () => {
|
|
135
|
+
throw new Error("statfs EIO");
|
|
136
|
+
},
|
|
137
|
+
room: () => {
|
|
138
|
+
throw new Error("statfs EIO");
|
|
139
|
+
},
|
|
140
|
+
warn: (line) => warned.push(line),
|
|
141
|
+
});
|
|
142
|
+
expect(() => r.found([w.file("tod/a.mp4", 1)])).not.toThrow();
|
|
143
|
+
expect(() => r.finish()).not.toThrow();
|
|
144
|
+
expect(warned).toHaveLength(1);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe("a dropped folder empties as it is filed", () => {
|
|
149
|
+
test("🔴 what an earlier sweep filed is carried, so the run's total does not shrink as the folder does", () => {
|
|
150
|
+
const w = world();
|
|
151
|
+
const one = w.file("Vacation/1.jpg", 10);
|
|
152
|
+
const two = w.file("Vacation/2.jpg", 20);
|
|
153
|
+
const first = w.reporter();
|
|
154
|
+
first.found([one, two]);
|
|
155
|
+
first.result(one, { outcome: "added", itemId: "i1" });
|
|
156
|
+
first.progress.waiting(two, "copying");
|
|
157
|
+
first.finish();
|
|
158
|
+
const startedAt = w.progress().sources[0]?.startedAt;
|
|
159
|
+
|
|
160
|
+
w.tick(300_000);
|
|
161
|
+
const second = w.reporter();
|
|
162
|
+
second.found([two]);
|
|
163
|
+
second.result(two, { outcome: "duplicate", itemId: "i0" });
|
|
164
|
+
second.finish();
|
|
165
|
+
|
|
166
|
+
const run = w.progress().sources[0];
|
|
167
|
+
expect(run).toMatchObject({ label: "Vacation", kind: "folder", startedAt, added: 1, duplicate: 1, total: { files: 2, bytes: 30 }, done: { files: 2, bytes: 30 } });
|
|
168
|
+
expect(run?.finishedAt).toBe("2026-10-22T12:05:00.000Z");
|
|
169
|
+
|
|
170
|
+
w.tick(300_000);
|
|
171
|
+
const third = w.reporter();
|
|
172
|
+
third.found([]);
|
|
173
|
+
third.finish();
|
|
174
|
+
expect(w.progress().sources[0]).toMatchObject({ label: "Vacation", finishedAt: "2026-10-22T12:05:00.000Z", total: { files: 2, bytes: 30 } });
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("a failed file is counted as failed, and the queue keeps it with its state", () => {
|
|
178
|
+
const w = world();
|
|
179
|
+
const bad = w.file("broken.mov", 7);
|
|
180
|
+
const r = w.reporter();
|
|
181
|
+
r.found([bad]);
|
|
182
|
+
r.progress.uploading(bad, "file/u_1");
|
|
183
|
+
r.result(bad, { outcome: "failed", detail: "500 boom" });
|
|
184
|
+
r.finish();
|
|
185
|
+
expect(w.progress().sources[0]).toMatchObject({ label: LOOSE_FILES, failed: { files: 1, bytes: 7 }, done: { files: 0, bytes: 0 } });
|
|
186
|
+
expect(w.lines<QueueLine>(QUEUE_FILE)[0]).toMatchObject({ path: "broken.mov", state: "failed" });
|
|
187
|
+
expect(w.lines<FileLine>(FILES_JOURNAL)[0]).toMatchObject({ outcome: "failed", detail: "500 boom" });
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test("a sweep that stopped on an error says so, and keeps every source as it was", () => {
|
|
191
|
+
const w = world();
|
|
192
|
+
const r1 = w.reporter();
|
|
193
|
+
r1.found([w.file("x.jpg", 1)]);
|
|
194
|
+
r1.finish();
|
|
195
|
+
const r2 = w.reporter();
|
|
196
|
+
r2.finish("accounts route answered 500");
|
|
197
|
+
const p = w.progress();
|
|
198
|
+
expect(p.sweep).toMatchObject({ state: "failed", error: "accounts route answered 500" });
|
|
199
|
+
expect(p.sources).toHaveLength(1);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe("sweepDropzone writes the report by itself (family's and music's sweeps)", () => {
|
|
204
|
+
const dropzone = () => join(root, "drop");
|
|
205
|
+
const put = (rel: string, bytes = 8): void => {
|
|
206
|
+
const full = join(dropzone(), rel);
|
|
207
|
+
mkdirSync(join(full, ".."), { recursive: true });
|
|
208
|
+
writeFileSync(full, Buffer.alloc(bytes, 7));
|
|
209
|
+
};
|
|
210
|
+
const reporter = () =>
|
|
211
|
+
createIngestReporter({
|
|
212
|
+
stateDir: join(root, "state"),
|
|
213
|
+
app: "family",
|
|
214
|
+
job: "com.family.ingest",
|
|
215
|
+
everySeconds: 300,
|
|
216
|
+
dropzone: dropzone(),
|
|
217
|
+
accounts: [{ id: "", label: "Family drop folder", dir: dropzone() }],
|
|
218
|
+
pid: 1,
|
|
219
|
+
});
|
|
220
|
+
const progress = (): IngestProgress => JSON.parse(readFileSync(join(root, "state", PROGRESS_FILE), "utf8")) as IngestProgress;
|
|
221
|
+
const journal = (): FileLine[] =>
|
|
222
|
+
readFileSync(join(root, "state", FILES_JOURNAL), "utf8")
|
|
223
|
+
.split("\n")
|
|
224
|
+
.filter(Boolean)
|
|
225
|
+
.map((l) => JSON.parse(l) as FileLine);
|
|
226
|
+
|
|
227
|
+
test("each album folder is a source, every outcome is journaled in the page's words, and the sweep's end is recorded", async () => {
|
|
228
|
+
put("Wedding 1998/a.jpg", 10);
|
|
229
|
+
put("Wedding 1998/b.jpg", 20);
|
|
230
|
+
put("Wedding 1998/notes.xyz", 3);
|
|
231
|
+
put("loose.jpg", 5);
|
|
232
|
+
put("maybe.jpg", 4);
|
|
233
|
+
const keys: string[] = [];
|
|
234
|
+
await sweepDropzone(
|
|
235
|
+
dropzone(),
|
|
236
|
+
async (file, ctx) => {
|
|
237
|
+
if (file.name === "notes.xyz") return { kind: "discard", detail: "not a photograph" };
|
|
238
|
+
if (file.name === "b.jpg") return { kind: "duplicate" };
|
|
239
|
+
if (file.name === "loose.jpg") return { kind: "failed", detail: "502 from prod" };
|
|
240
|
+
if (file.name === "maybe.jpg") return { kind: "held", detail: "looks like real/media/x" };
|
|
241
|
+
ctx.uploading(`real/media/${file.name}`);
|
|
242
|
+
keys.push(progress().sources.find((s) => s.current)?.current?.key ?? "");
|
|
243
|
+
return { kind: "stored" };
|
|
244
|
+
},
|
|
245
|
+
{ stabilityMs: 0, report: reporter() },
|
|
246
|
+
);
|
|
247
|
+
// The handler named its key while the file was going in — the page's live line for that file.
|
|
248
|
+
expect(keys).toEqual(["real/media/a.jpg"]);
|
|
249
|
+
const p = progress();
|
|
250
|
+
expect(p.sweep).toMatchObject({ state: "finished", phase: "done" });
|
|
251
|
+
expect(p.sources.find((s) => s.label === "Wedding 1998")).toMatchObject({ kind: "folder", added: 1, duplicate: 1, blocked: 1, total: { files: 3, bytes: 33 } });
|
|
252
|
+
// A held file is left alone, not an event: it is shown as skipped and never journaled, so the
|
|
253
|
+
// sweep that meets it again in five minutes does not write it again.
|
|
254
|
+
expect(p.sources.find((s) => s.label === LOOSE_FILES)).toMatchObject({ failed: { files: 1, bytes: 5 }, skipped: { files: 1, bytes: 4 }, parked: 0 });
|
|
255
|
+
expect(journal().map((l) => [l.path, l.outcome, l.detail ?? null]).sort()).toEqual([
|
|
256
|
+
["Wedding 1998/a.jpg", "added", null],
|
|
257
|
+
["Wedding 1998/b.jpg", "duplicate", null],
|
|
258
|
+
["Wedding 1998/notes.xyz", "blocked", "not a photograph"],
|
|
259
|
+
["loose.jpg", "failed", "502 from prod"],
|
|
260
|
+
]);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("a dry run walks and journals nothing, and a sweep that throws records why before it rethrows", async () => {
|
|
264
|
+
put("a.jpg");
|
|
265
|
+
await sweepDropzone(dropzone(), async () => ({ kind: "stored" }), { stabilityMs: 0, dryRun: true, report: reporter() });
|
|
266
|
+
expect(progress()).toMatchObject({ sweep: { phase: "starting" }, sources: [] });
|
|
267
|
+
expect(existsSync(join(root, "state", FILES_JOURNAL))).toBe(false);
|
|
268
|
+
|
|
269
|
+
const onProgress = () => {
|
|
270
|
+
throw new Error("the store went away");
|
|
271
|
+
};
|
|
272
|
+
await expect(sweepDropzone(dropzone(), async () => ({ kind: "stored" }), { stabilityMs: 0, onProgress, report: reporter() })).rejects.toThrow();
|
|
273
|
+
expect(progress().sweep).toMatchObject({ state: "failed", error: "the store went away" });
|
|
274
|
+
});
|
|
275
|
+
});
|