@g9tm/app-diagnostic 0.2.0 → 0.2.2
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/package.json +20 -12
- package/src/bytes.ts +48 -48
- package/src/host.ts +124 -124
- package/src/index.ts +30 -30
- package/src/live/bundle.ts +77 -77
- package/src/live/channel.ts +496 -496
- package/src/live/handlers.ts +426 -426
- package/src/live/models.ts +366 -360
- package/src/live/upload.ts +175 -175
- package/src/recording.ts +176 -176
- package/src/store.ts +39 -39
- package/src/strings.generated.ts +242 -242
- package/src/strings.ts +37 -37
- package/src/tester/archive.ts +83 -83
- package/src/tester/compose.ts +99 -99
- package/src/tester/engine.ts +2 -0
- package/src/tester/feed.ts +182 -182
- package/src/tester/gesture.ts +61 -61
- package/src/tester/model.ts +125 -125
- package/src/tester/outbox.ts +131 -131
- package/src/tester/screen.ts +117 -117
- package/src/tester/store.ts +145 -145
- package/src/tester/timeline.ts +98 -98
- package/src/upload.ts +236 -236
- package/src/wire.ts +133 -133
package/src/tester/model.ts
CHANGED
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
// A tester (QA) report on the device — the TypeScript twin of G9AppDiagnostic.Maui TesterReportModels. The JSON shape of
|
|
2
|
-
// `report.json` is the .NET one, key for key and kind for kind (numbers): the gateway, its operator page and the local AI
|
|
3
|
-
// read the archives of every app, so a report from this package is indistinguishable from a .NET one. Add, never rename.
|
|
4
|
-
|
|
5
|
-
/** What a tester captured; the name is the folder inside the report (`<report>/<Kind>/`), the number its value in JSON. */
|
|
6
|
-
export const TesterAttachmentKind = {
|
|
7
|
-
Screenshot: 0,
|
|
8
|
-
Recording: 1,
|
|
9
|
-
Voice: 2,
|
|
10
|
-
File: 3,
|
|
11
|
-
/** Collected by the app, never by the tester (context, logs, a recording's timeline). */
|
|
12
|
-
Diagnostic: 4,
|
|
13
|
-
/** The local-store backup, only when the tester opts in on the send form. */
|
|
14
|
-
DatabaseBackup: 5,
|
|
15
|
-
} as const;
|
|
16
|
-
export type TesterAttachmentKind = (typeof TesterAttachmentKind)[keyof typeof TesterAttachmentKind];
|
|
17
|
-
|
|
18
|
-
const KIND_NAMES = ['Screenshot', 'Recording', 'Voice', 'File', 'Diagnostic', 'DatabaseBackup'] as const;
|
|
19
|
-
|
|
20
|
-
/** The folder name of a kind (.NET `nameof(TesterAttachmentKind.X)`); an unknown number reads as a file. */
|
|
21
|
-
export function kindFolder(kind: number): string {
|
|
22
|
-
return KIND_NAMES[kind] ?? 'File';
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface TesterReportAttachment {
|
|
26
|
-
readonly id: string;
|
|
27
|
-
readonly kind: TesterAttachmentKind;
|
|
28
|
-
/** The file name inside its kind folder — never an absolute path (app data paths move across reinstalls). */
|
|
29
|
-
readonly fileName: string;
|
|
30
|
-
readonly displayName: string;
|
|
31
|
-
readonly sizeBytes: number;
|
|
32
|
-
readonly capturedUtc: string;
|
|
33
|
-
/** When a recording STARTED (a screen recording, its sound, its timeline): the local AI aligns them by these. */
|
|
34
|
-
readonly startedUtc?: string;
|
|
35
|
-
/** The `id` of the screen recording this file belongs to (its sound, its timeline). */
|
|
36
|
-
readonly pairedWith?: string;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Where the tester was when the report started, as values (report.json `screen`; the app's own facts beside them). */
|
|
40
|
-
export interface TesterScreenSnapshot {
|
|
41
|
-
readonly section?: string | null;
|
|
42
|
-
readonly openSheets: number;
|
|
43
|
-
readonly online: boolean;
|
|
44
|
-
readonly viewPath: readonly string[];
|
|
45
|
-
readonly screenNames: readonly string[];
|
|
46
|
-
readonly capturedUtc: string;
|
|
47
|
-
readonly [extra: string]: unknown;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface TesterReportDraft {
|
|
51
|
-
readonly id: string;
|
|
52
|
-
/** Becomes the tracker issue's summary (the app's own title since 2026-10-31: there is no title field). */
|
|
53
|
-
readonly title: string;
|
|
54
|
-
readonly description: string;
|
|
55
|
-
readonly createdUtc: string;
|
|
56
|
-
readonly attachments: readonly TesterReportAttachment[];
|
|
57
|
-
/** The opt-in for the database backup (off by default: the one thing that makes a report huge). */
|
|
58
|
-
readonly includeDatabaseBackup: boolean;
|
|
59
|
-
/** Who started it: the outbox uploads later, maybe after a sign-out or an impersonation switch. */
|
|
60
|
-
readonly username: string | null;
|
|
61
|
-
/** false = never to the gateway's local AI; absent = the gateway decides. */
|
|
62
|
-
readonly aiPack?: boolean | null;
|
|
63
|
-
/** The report's kind key (`bug`, `feature`…); absent = the first kind. */
|
|
64
|
-
readonly kind?: string | null;
|
|
65
|
-
/** The build's git branch and commit, set when archived. */
|
|
66
|
-
readonly branch?: string | null;
|
|
67
|
-
readonly commit?: string | null;
|
|
68
|
-
/** Where the report started, for context.txt (English lines). */
|
|
69
|
-
readonly screenContext?: readonly string[] | null;
|
|
70
|
-
/** The title the APP wrote («a problem in …»), so a reader tells it from the tester's words. */
|
|
71
|
-
readonly autoTitle?: string | null;
|
|
72
|
-
/** The labelled block the app put at the head of the description. */
|
|
73
|
-
readonly autoDescription?: string | null;
|
|
74
|
-
readonly screen?: TesterScreenSnapshot | null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Pieces of evidence as the tester thinks of them: a recording's own sound and timeline are part of the video. */
|
|
78
|
-
export function evidence(draft: TesterReportDraft | null): readonly TesterReportAttachment[] {
|
|
79
|
-
return draft ? draft.attachments.filter((a) => !a.pairedWith && a.kind !== TesterAttachmentKind.Diagnostic && a.kind !== TesterAttachmentKind.DatabaseBackup) : [];
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** The send form's chips: how many of each kind. */
|
|
83
|
-
export function evidenceCounts(draft: TesterReportDraft | null): ReadonlyMap<TesterAttachmentKind, number> {
|
|
84
|
-
const counts = new Map<TesterAttachmentKind, number>();
|
|
85
|
-
for (const a of evidence(draft)) counts.set(a.kind, (counts.get(a.kind) ?? 0) + 1);
|
|
86
|
-
return counts;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function isDraft(value: unknown): value is TesterReportDraft {
|
|
90
|
-
const v = value as Partial<TesterReportDraft> | null;
|
|
91
|
-
return !!v && typeof v.id === 'string' && typeof v.title === 'string' && Array.isArray(v.attachments);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** 32 lower-case hex characters (.NET `Guid.NewGuid().ToString("N")` — the gateway's ids are hex). */
|
|
95
|
-
export function newReportId(random: () => number = Math.random): string {
|
|
96
|
-
let s = '';
|
|
97
|
-
for (let i = 0; i < 32; i++) s += Math.floor(random() * 16).toString(16);
|
|
98
|
-
return s;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** A file name that is safe inside a kind folder (no separators, no leading dots), keeping the extension. */
|
|
102
|
-
export function safeFileName(name: string): string {
|
|
103
|
-
const base = name.split(/[\\/]/).pop() ?? 'file';
|
|
104
|
-
// An explicit unsafe set, not \p{L}: PrimJS (the Lynx engine) rejects Unicode property escapes at bytecode time.
|
|
105
|
-
const cleaned = base.replace(/[\x00-\x1f\x7f<>:"|?*]/g, '_').replace(/^\.+/, '').trim();
|
|
106
|
-
return cleaned.length > 0 ? cleaned.slice(-120) : 'file';
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** `name.ext` → `name (2).ext` until `taken` says no. */
|
|
110
|
-
export function uniqueName(name: string, taken: (candidate: string) => boolean): string {
|
|
111
|
-
if (!taken(name)) return name;
|
|
112
|
-
const dot = name.lastIndexOf('.');
|
|
113
|
-
const stem = dot > 0 ? name.slice(0, dot) : name;
|
|
114
|
-
const ext = dot > 0 ? name.slice(dot) : '';
|
|
115
|
-
for (let i = 2; ; i++) {
|
|
116
|
-
const candidate = `${stem} (${i})${ext}`;
|
|
117
|
-
if (!taken(candidate)) return candidate;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function formatBytes(bytes: number): string {
|
|
122
|
-
if (bytes < 1024) return `${bytes} B`;
|
|
123
|
-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB`;
|
|
124
|
-
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
125
|
-
}
|
|
1
|
+
// A tester (QA) report on the device — the TypeScript twin of G9AppDiagnostic.Maui TesterReportModels. The JSON shape of
|
|
2
|
+
// `report.json` is the .NET one, key for key and kind for kind (numbers): the gateway, its operator page and the local AI
|
|
3
|
+
// read the archives of every app, so a report from this package is indistinguishable from a .NET one. Add, never rename.
|
|
4
|
+
|
|
5
|
+
/** What a tester captured; the name is the folder inside the report (`<report>/<Kind>/`), the number its value in JSON. */
|
|
6
|
+
export const TesterAttachmentKind = {
|
|
7
|
+
Screenshot: 0,
|
|
8
|
+
Recording: 1,
|
|
9
|
+
Voice: 2,
|
|
10
|
+
File: 3,
|
|
11
|
+
/** Collected by the app, never by the tester (context, logs, a recording's timeline). */
|
|
12
|
+
Diagnostic: 4,
|
|
13
|
+
/** The local-store backup, only when the tester opts in on the send form. */
|
|
14
|
+
DatabaseBackup: 5,
|
|
15
|
+
} as const;
|
|
16
|
+
export type TesterAttachmentKind = (typeof TesterAttachmentKind)[keyof typeof TesterAttachmentKind];
|
|
17
|
+
|
|
18
|
+
const KIND_NAMES = ['Screenshot', 'Recording', 'Voice', 'File', 'Diagnostic', 'DatabaseBackup'] as const;
|
|
19
|
+
|
|
20
|
+
/** The folder name of a kind (.NET `nameof(TesterAttachmentKind.X)`); an unknown number reads as a file. */
|
|
21
|
+
export function kindFolder(kind: number): string {
|
|
22
|
+
return KIND_NAMES[kind] ?? 'File';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface TesterReportAttachment {
|
|
26
|
+
readonly id: string;
|
|
27
|
+
readonly kind: TesterAttachmentKind;
|
|
28
|
+
/** The file name inside its kind folder — never an absolute path (app data paths move across reinstalls). */
|
|
29
|
+
readonly fileName: string;
|
|
30
|
+
readonly displayName: string;
|
|
31
|
+
readonly sizeBytes: number;
|
|
32
|
+
readonly capturedUtc: string;
|
|
33
|
+
/** When a recording STARTED (a screen recording, its sound, its timeline): the local AI aligns them by these. */
|
|
34
|
+
readonly startedUtc?: string;
|
|
35
|
+
/** The `id` of the screen recording this file belongs to (its sound, its timeline). */
|
|
36
|
+
readonly pairedWith?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Where the tester was when the report started, as values (report.json `screen`; the app's own facts beside them). */
|
|
40
|
+
export interface TesterScreenSnapshot {
|
|
41
|
+
readonly section?: string | null;
|
|
42
|
+
readonly openSheets: number;
|
|
43
|
+
readonly online: boolean;
|
|
44
|
+
readonly viewPath: readonly string[];
|
|
45
|
+
readonly screenNames: readonly string[];
|
|
46
|
+
readonly capturedUtc: string;
|
|
47
|
+
readonly [extra: string]: unknown;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface TesterReportDraft {
|
|
51
|
+
readonly id: string;
|
|
52
|
+
/** Becomes the tracker issue's summary (the app's own title since 2026-10-31: there is no title field). */
|
|
53
|
+
readonly title: string;
|
|
54
|
+
readonly description: string;
|
|
55
|
+
readonly createdUtc: string;
|
|
56
|
+
readonly attachments: readonly TesterReportAttachment[];
|
|
57
|
+
/** The opt-in for the database backup (off by default: the one thing that makes a report huge). */
|
|
58
|
+
readonly includeDatabaseBackup: boolean;
|
|
59
|
+
/** Who started it: the outbox uploads later, maybe after a sign-out or an impersonation switch. */
|
|
60
|
+
readonly username: string | null;
|
|
61
|
+
/** false = never to the gateway's local AI; absent = the gateway decides. */
|
|
62
|
+
readonly aiPack?: boolean | null;
|
|
63
|
+
/** The report's kind key (`bug`, `feature`…); absent = the first kind. */
|
|
64
|
+
readonly kind?: string | null;
|
|
65
|
+
/** The build's git branch and commit, set when archived. */
|
|
66
|
+
readonly branch?: string | null;
|
|
67
|
+
readonly commit?: string | null;
|
|
68
|
+
/** Where the report started, for context.txt (English lines). */
|
|
69
|
+
readonly screenContext?: readonly string[] | null;
|
|
70
|
+
/** The title the APP wrote («a problem in …»), so a reader tells it from the tester's words. */
|
|
71
|
+
readonly autoTitle?: string | null;
|
|
72
|
+
/** The labelled block the app put at the head of the description. */
|
|
73
|
+
readonly autoDescription?: string | null;
|
|
74
|
+
readonly screen?: TesterScreenSnapshot | null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Pieces of evidence as the tester thinks of them: a recording's own sound and timeline are part of the video. */
|
|
78
|
+
export function evidence(draft: TesterReportDraft | null): readonly TesterReportAttachment[] {
|
|
79
|
+
return draft ? draft.attachments.filter((a) => !a.pairedWith && a.kind !== TesterAttachmentKind.Diagnostic && a.kind !== TesterAttachmentKind.DatabaseBackup) : [];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The send form's chips: how many of each kind. */
|
|
83
|
+
export function evidenceCounts(draft: TesterReportDraft | null): ReadonlyMap<TesterAttachmentKind, number> {
|
|
84
|
+
const counts = new Map<TesterAttachmentKind, number>();
|
|
85
|
+
for (const a of evidence(draft)) counts.set(a.kind, (counts.get(a.kind) ?? 0) + 1);
|
|
86
|
+
return counts;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function isDraft(value: unknown): value is TesterReportDraft {
|
|
90
|
+
const v = value as Partial<TesterReportDraft> | null;
|
|
91
|
+
return !!v && typeof v.id === 'string' && typeof v.title === 'string' && Array.isArray(v.attachments);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 32 lower-case hex characters (.NET `Guid.NewGuid().ToString("N")` — the gateway's ids are hex). */
|
|
95
|
+
export function newReportId(random: () => number = Math.random): string {
|
|
96
|
+
let s = '';
|
|
97
|
+
for (let i = 0; i < 32; i++) s += Math.floor(random() * 16).toString(16);
|
|
98
|
+
return s;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A file name that is safe inside a kind folder (no separators, no leading dots), keeping the extension. */
|
|
102
|
+
export function safeFileName(name: string): string {
|
|
103
|
+
const base = name.split(/[\\/]/).pop() ?? 'file';
|
|
104
|
+
// An explicit unsafe set, not \p{L}: PrimJS (the Lynx engine) rejects Unicode property escapes at bytecode time.
|
|
105
|
+
const cleaned = base.replace(/[\x00-\x1f\x7f<>:"|?*]/g, '_').replace(/^\.+/, '').trim();
|
|
106
|
+
return cleaned.length > 0 ? cleaned.slice(-120) : 'file';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** `name.ext` → `name (2).ext` until `taken` says no. */
|
|
110
|
+
export function uniqueName(name: string, taken: (candidate: string) => boolean): string {
|
|
111
|
+
if (!taken(name)) return name;
|
|
112
|
+
const dot = name.lastIndexOf('.');
|
|
113
|
+
const stem = dot > 0 ? name.slice(0, dot) : name;
|
|
114
|
+
const ext = dot > 0 ? name.slice(dot) : '';
|
|
115
|
+
for (let i = 2; ; i++) {
|
|
116
|
+
const candidate = `${stem} (${i})${ext}`;
|
|
117
|
+
if (!taken(candidate)) return candidate;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function formatBytes(bytes: number): string {
|
|
122
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
123
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB`;
|
|
124
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
125
|
+
}
|
package/src/tester/outbox.ts
CHANGED
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
// The tester report outbox (G9AppDiagnostic.Maui TesterReportOutboxService): a folder of `*.zip`, uploaded to the
|
|
2
|
-
// gateway and deleted locally once — and only once — the gateway confirmed the WHOLE report.
|
|
3
|
-
//
|
|
4
|
-
// The folder IS the queue: a zip there means "not on the gateway yet", nothing else keeps state, so nothing can disagree
|
|
5
|
-
// with it and a force-close between any two steps leaves the queue as correct as before. Three triggers, one
|
|
6
|
-
// single-flight drain: the periodic loop (the level trigger: a flaky link eventually gets through), the network coming
|
|
7
|
-
// back (the edge trigger), and the tester's "send it now". The automatic triggers skip while a drain runs; the manual
|
|
8
|
-
// one WAITS for it, so it reports what really happened instead of "busy".
|
|
9
|
-
import { type DiagnosticFiles, type DiagnosticLog, errorText, silentLog } from '../host.ts';
|
|
10
|
-
import { createStore, type ReadableStore } from '../store.ts';
|
|
11
|
-
|
|
12
|
-
export interface OutboxState {
|
|
13
|
-
readonly pending: number;
|
|
14
|
-
readonly draining: boolean;
|
|
15
|
-
/** Reports this drain has uploaded so far. */
|
|
16
|
-
readonly sent: number;
|
|
17
|
-
readonly lastFailure: string | null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface OutboxOptions {
|
|
21
|
-
readonly files: DiagnosticFiles;
|
|
22
|
-
/** The outbox folder (absolute). */
|
|
23
|
-
readonly dir: () => Promise<string>;
|
|
24
|
-
/** Uploads one archive; true ONLY when the gateway has all of it. */
|
|
25
|
-
readonly upload: (path: string) => Promise<boolean>;
|
|
26
|
-
readonly online: () => Promise<boolean>;
|
|
27
|
-
/** Default 3 minutes (MAUI's loop). */
|
|
28
|
-
readonly intervalMs?: number;
|
|
29
|
-
/** Holds the process while a drain sends (a foreground service); its release is called after. */
|
|
30
|
-
readonly keepAlive?: () => Promise<(() => void | Promise<void>) | null>;
|
|
31
|
-
readonly log?: DiagnosticLog;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const nameOf = (p: string) => p.split('/').pop() ?? p;
|
|
35
|
-
|
|
36
|
-
export class TesterOutbox {
|
|
37
|
-
private readonly o: OutboxOptions;
|
|
38
|
-
private readonly store = createStore<OutboxState>({ pending: 0, draining: false, sent: 0, lastFailure: null });
|
|
39
|
-
private running: Promise<number> | null = null;
|
|
40
|
-
private timer: ReturnType<typeof setInterval> | null = null;
|
|
41
|
-
|
|
42
|
-
constructor(options: OutboxOptions) {
|
|
43
|
-
this.o = options;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
get state(): ReadableStore<OutboxState> {
|
|
47
|
-
return this.store;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async queued(): Promise<string[]> {
|
|
51
|
-
try {
|
|
52
|
-
const dir = await this.o.dir();
|
|
53
|
-
return (await this.o.files.list(dir)).filter((f) => !f.dir && f.name.endsWith('.zip')).map((f) => f.path);
|
|
54
|
-
} catch {
|
|
55
|
-
return [];
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async refresh(): Promise<number> {
|
|
60
|
-
const pending = (await this.queued()).length;
|
|
61
|
-
// an empty queue has no failure to report (the failed file was sent by a later pass, or removed)
|
|
62
|
-
this.store.set((s) => ({ ...s, pending, ...(pending === 0 ? { lastFailure: null } : {}) }));
|
|
63
|
-
return pending;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** The level trigger + one immediate pass. Idempotent. */
|
|
67
|
-
start(): void {
|
|
68
|
-
if (this.timer) return;
|
|
69
|
-
this.timer = setInterval(() => void this.drain(false), this.o.intervalMs ?? 3 * 60_000);
|
|
70
|
-
void this.drain(false);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
stop(): void {
|
|
74
|
-
if (this.timer) clearInterval(this.timer);
|
|
75
|
-
this.timer = null;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** A report just landed in the outbox (a foreground moment): upload now. */
|
|
79
|
-
notifyQueued(): void {
|
|
80
|
-
void this.refresh().then(() => this.drain(false));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** The tester's "send now": waits for a drain in flight, then sends what is left. Resolves how many went. */
|
|
84
|
-
async flush(): Promise<number> {
|
|
85
|
-
if (this.running) await this.running.catch(() => 0);
|
|
86
|
-
return this.drain(true);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
drain(manual: boolean): Promise<number> {
|
|
90
|
-
if (this.running) return manual ? this.running.then(() => this.drain(true)) : Promise.resolve(0);
|
|
91
|
-
const run = this.drainOnce().finally(() => {
|
|
92
|
-
this.running = null;
|
|
93
|
-
this.store.set((s) => ({ ...s, draining: false }));
|
|
94
|
-
});
|
|
95
|
-
this.running = run;
|
|
96
|
-
return run;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private async drainOnce(): Promise<number> {
|
|
100
|
-
const log = this.o.log ?? silentLog;
|
|
101
|
-
const queue = await this.queued();
|
|
102
|
-
this.store.set((s) => ({ ...s, pending: queue.length, ...(queue.length === 0 ? { lastFailure: null } : {}) }));
|
|
103
|
-
if (queue.length === 0) return 0;
|
|
104
|
-
if (!(await this.o.online().catch(() => true))) return 0;
|
|
105
|
-
this.store.set((s) => ({ ...s, draining: true, sent: 0, lastFailure: null }));
|
|
106
|
-
const release = await this.o.keepAlive?.().catch(() => null);
|
|
107
|
-
let sent = 0;
|
|
108
|
-
try {
|
|
109
|
-
for (const path of queue) {
|
|
110
|
-
let ok = false;
|
|
111
|
-
try {
|
|
112
|
-
ok = await this.o.upload(path);
|
|
113
|
-
} catch (error) {
|
|
114
|
-
log.log('warn', 'tester', `outbox upload threw: ${errorText(error)}`);
|
|
115
|
-
}
|
|
116
|
-
if (!ok) {
|
|
117
|
-
this.store.set((s) => ({ ...s, lastFailure: nameOf(path) }));
|
|
118
|
-
continue; // keep it: the next pass retries
|
|
119
|
-
}
|
|
120
|
-
await this.o.files.remove(path).catch(() => {});
|
|
121
|
-
sent++;
|
|
122
|
-
this.store.set((s) => ({ ...s, sent, pending: Math.max(0, s.pending - 1) }));
|
|
123
|
-
log.log('info', 'tester', `report ${nameOf(path)} uploaded`);
|
|
124
|
-
}
|
|
125
|
-
} finally {
|
|
126
|
-
if (release) await Promise.resolve(release()).catch(() => {});
|
|
127
|
-
}
|
|
128
|
-
await this.refresh();
|
|
129
|
-
return sent;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
1
|
+
// The tester report outbox (G9AppDiagnostic.Maui TesterReportOutboxService): a folder of `*.zip`, uploaded to the
|
|
2
|
+
// gateway and deleted locally once — and only once — the gateway confirmed the WHOLE report.
|
|
3
|
+
//
|
|
4
|
+
// The folder IS the queue: a zip there means "not on the gateway yet", nothing else keeps state, so nothing can disagree
|
|
5
|
+
// with it and a force-close between any two steps leaves the queue as correct as before. Three triggers, one
|
|
6
|
+
// single-flight drain: the periodic loop (the level trigger: a flaky link eventually gets through), the network coming
|
|
7
|
+
// back (the edge trigger), and the tester's "send it now". The automatic triggers skip while a drain runs; the manual
|
|
8
|
+
// one WAITS for it, so it reports what really happened instead of "busy".
|
|
9
|
+
import { type DiagnosticFiles, type DiagnosticLog, errorText, silentLog } from '../host.ts';
|
|
10
|
+
import { createStore, type ReadableStore } from '../store.ts';
|
|
11
|
+
|
|
12
|
+
export interface OutboxState {
|
|
13
|
+
readonly pending: number;
|
|
14
|
+
readonly draining: boolean;
|
|
15
|
+
/** Reports this drain has uploaded so far. */
|
|
16
|
+
readonly sent: number;
|
|
17
|
+
readonly lastFailure: string | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface OutboxOptions {
|
|
21
|
+
readonly files: DiagnosticFiles;
|
|
22
|
+
/** The outbox folder (absolute). */
|
|
23
|
+
readonly dir: () => Promise<string>;
|
|
24
|
+
/** Uploads one archive; true ONLY when the gateway has all of it. */
|
|
25
|
+
readonly upload: (path: string) => Promise<boolean>;
|
|
26
|
+
readonly online: () => Promise<boolean>;
|
|
27
|
+
/** Default 3 minutes (MAUI's loop). */
|
|
28
|
+
readonly intervalMs?: number;
|
|
29
|
+
/** Holds the process while a drain sends (a foreground service); its release is called after. */
|
|
30
|
+
readonly keepAlive?: () => Promise<(() => void | Promise<void>) | null>;
|
|
31
|
+
readonly log?: DiagnosticLog;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const nameOf = (p: string) => p.split('/').pop() ?? p;
|
|
35
|
+
|
|
36
|
+
export class TesterOutbox {
|
|
37
|
+
private readonly o: OutboxOptions;
|
|
38
|
+
private readonly store = createStore<OutboxState>({ pending: 0, draining: false, sent: 0, lastFailure: null });
|
|
39
|
+
private running: Promise<number> | null = null;
|
|
40
|
+
private timer: ReturnType<typeof setInterval> | null = null;
|
|
41
|
+
|
|
42
|
+
constructor(options: OutboxOptions) {
|
|
43
|
+
this.o = options;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get state(): ReadableStore<OutboxState> {
|
|
47
|
+
return this.store;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async queued(): Promise<string[]> {
|
|
51
|
+
try {
|
|
52
|
+
const dir = await this.o.dir();
|
|
53
|
+
return (await this.o.files.list(dir)).filter((f) => !f.dir && f.name.endsWith('.zip')).map((f) => f.path);
|
|
54
|
+
} catch {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async refresh(): Promise<number> {
|
|
60
|
+
const pending = (await this.queued()).length;
|
|
61
|
+
// an empty queue has no failure to report (the failed file was sent by a later pass, or removed)
|
|
62
|
+
this.store.set((s) => ({ ...s, pending, ...(pending === 0 ? { lastFailure: null } : {}) }));
|
|
63
|
+
return pending;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The level trigger + one immediate pass. Idempotent. */
|
|
67
|
+
start(): void {
|
|
68
|
+
if (this.timer) return;
|
|
69
|
+
this.timer = setInterval(() => void this.drain(false), this.o.intervalMs ?? 3 * 60_000);
|
|
70
|
+
void this.drain(false);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
stop(): void {
|
|
74
|
+
if (this.timer) clearInterval(this.timer);
|
|
75
|
+
this.timer = null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A report just landed in the outbox (a foreground moment): upload now. */
|
|
79
|
+
notifyQueued(): void {
|
|
80
|
+
void this.refresh().then(() => this.drain(false));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The tester's "send now": waits for a drain in flight, then sends what is left. Resolves how many went. */
|
|
84
|
+
async flush(): Promise<number> {
|
|
85
|
+
if (this.running) await this.running.catch(() => 0);
|
|
86
|
+
return this.drain(true);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
drain(manual: boolean): Promise<number> {
|
|
90
|
+
if (this.running) return manual ? this.running.then(() => this.drain(true)) : Promise.resolve(0);
|
|
91
|
+
const run = this.drainOnce().finally(() => {
|
|
92
|
+
this.running = null;
|
|
93
|
+
this.store.set((s) => ({ ...s, draining: false }));
|
|
94
|
+
});
|
|
95
|
+
this.running = run;
|
|
96
|
+
return run;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private async drainOnce(): Promise<number> {
|
|
100
|
+
const log = this.o.log ?? silentLog;
|
|
101
|
+
const queue = await this.queued();
|
|
102
|
+
this.store.set((s) => ({ ...s, pending: queue.length, ...(queue.length === 0 ? { lastFailure: null } : {}) }));
|
|
103
|
+
if (queue.length === 0) return 0;
|
|
104
|
+
if (!(await this.o.online().catch(() => true))) return 0;
|
|
105
|
+
this.store.set((s) => ({ ...s, draining: true, sent: 0, lastFailure: null }));
|
|
106
|
+
const release = await this.o.keepAlive?.().catch(() => null);
|
|
107
|
+
let sent = 0;
|
|
108
|
+
try {
|
|
109
|
+
for (const path of queue) {
|
|
110
|
+
let ok = false;
|
|
111
|
+
try {
|
|
112
|
+
ok = await this.o.upload(path);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
log.log('warn', 'tester', `outbox upload threw: ${errorText(error)}`);
|
|
115
|
+
}
|
|
116
|
+
if (!ok) {
|
|
117
|
+
this.store.set((s) => ({ ...s, lastFailure: nameOf(path) }));
|
|
118
|
+
continue; // keep it: the next pass retries
|
|
119
|
+
}
|
|
120
|
+
await this.o.files.remove(path).catch(() => {});
|
|
121
|
+
sent++;
|
|
122
|
+
this.store.set((s) => ({ ...s, sent, pending: Math.max(0, s.pending - 1) }));
|
|
123
|
+
log.log('info', 'tester', `report ${nameOf(path)} uploaded`);
|
|
124
|
+
}
|
|
125
|
+
} finally {
|
|
126
|
+
if (release) await Promise.resolve(release()).catch(() => {});
|
|
127
|
+
}
|
|
128
|
+
await this.refresh();
|
|
129
|
+
return sent;
|
|
130
|
+
}
|
|
131
|
+
}
|