Add credential references for steps: field ids, addUse/findFieldRef, usesView (Refs #24)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 12:35:22 -05:00
parent f089a00b66
commit edb3e25e67
5 changed files with 110 additions and 2 deletions

View File

@@ -31,7 +31,12 @@ import { isImageDataUrl, MAX_IMAGE_DATA_CHARS } from './images.js';
// frames: [Frame] } -- a numbered series of pictures with optional text. // frames: [Frame] } -- a numbered series of pictures with optional text.
// Frame: { image: PNG/JPEG data URL or '', text, layout: 'right'|'under'|'none', secret?: boolean, // Frame: { image: PNG/JPEG data URL or '', text, layout: 'right'|'under'|'none', secret?: boolean,
// ifStuck?: string, what to do if the screen looks different or the step fails // ifStuck?: string, what to do if the screen looks different or the step fails
// stop?: boolean } "stop here and call someone" marker; ifStuck says who/why // stop?: boolean, "stop here and call someone" marker; ifStuck says who/why
// uses?: [{ record: <record id>, field: <field id> }] credentials this step needs,
// shown beside the step with Show/Hide and Copy. A reference
// that no longer resolves is shown as missing, never an error.
// A record Field may carry `id` (unique within its record): assigned the first time a step
// references it, so the reference survives reordering and renaming the field.
// A sequence may also carry `captured: 'YYYY-MM-DD'`, set when a step is captured from the screen. // A sequence may also carry `captured: 'YYYY-MM-DD'`, set when a step is captured from the screen.
// A sequence lives in the same ordered list as the records (records sections) or // A sequence lives in the same ordered list as the records (records sections) or
// items (steps / checklist sections), so it sits wherever the user puts it, but it // items (steps / checklist sections), so it sits wherever the user puts it, but it
@@ -168,12 +173,18 @@ export function validate(doc) {
if (!DATE_RE.test(r.updated ?? '')) errs.push(`${where}: updated must be YYYY-MM-DD`); if (!DATE_RE.test(r.updated ?? '')) errs.push(`${where}: updated must be YYYY-MM-DD`);
if (r.notes !== undefined && !isStr(r.notes)) errs.push(`${where}: notes must be a string`); if (r.notes !== undefined && !isStr(r.notes)) errs.push(`${where}: notes must be a string`);
if (!Array.isArray(r.fields)) return errs.push(`${where}: fields must be an array`); if (!Array.isArray(r.fields)) return errs.push(`${where}: fields must be an array`);
const fieldIds = new Set();
r.fields.forEach((f, k) => { r.fields.forEach((f, k) => {
const fw = `${where}.fields[${k}]`; const fw = `${where}.fields[${k}]`;
if (!isObj(f)) return errs.push(`${fw}: must be an object`); if (!isObj(f)) return errs.push(`${fw}: must be an object`);
if (!isStr(f.label) || !f.label) errs.push(`${fw}: missing label`); if (!isStr(f.label) || !f.label) errs.push(`${fw}: missing label`);
if (!isStr(f.value)) errs.push(`${fw}: value must be a string`); if (!isStr(f.value)) errs.push(`${fw}: value must be a string`);
if (f.secret !== undefined && typeof f.secret !== 'boolean') errs.push(`${fw}: secret must be a boolean`); if (f.secret !== undefined && typeof f.secret !== 'boolean') errs.push(`${fw}: secret must be a boolean`);
if (f.id !== undefined) {
if (!isStr(f.id) || !f.id) errs.push(`${fw}: id must be a non-empty string`);
else if (fieldIds.has(f.id)) errs.push(`${fw}: duplicate field id "${f.id}"`);
else fieldIds.add(f.id);
}
if (f.kind !== undefined && !FIELD_KINDS.includes(f.kind)) errs.push(`${fw}: unknown kind "${f.kind}"`); if (f.kind !== undefined && !FIELD_KINDS.includes(f.kind)) errs.push(`${fw}: unknown kind "${f.kind}"`);
if (f.kind === 'image' && isStr(f.value) && f.value !== '' && !isImageDataUrl(f.value)) { if (f.kind === 'image' && isStr(f.value) && f.value !== '' && !isImageDataUrl(f.value)) {
errs.push(`${fw}: image must be a PNG or JPEG under ${Math.round(MAX_IMAGE_DATA_CHARS / 1e6)} MB`); errs.push(`${fw}: image must be a PNG or JPEG under ${Math.round(MAX_IMAGE_DATA_CHARS / 1e6)} MB`);
@@ -199,6 +210,12 @@ export function validate(doc) {
if (f.secret !== undefined && typeof f.secret !== 'boolean') errs.push(`${fw}: secret must be a boolean`); if (f.secret !== undefined && typeof f.secret !== 'boolean') errs.push(`${fw}: secret must be a boolean`);
if (f.ifStuck !== undefined && !isStr(f.ifStuck)) errs.push(`${fw}: ifStuck must be a string`); if (f.ifStuck !== undefined && !isStr(f.ifStuck)) errs.push(`${fw}: ifStuck must be a string`);
if (f.stop !== undefined && typeof f.stop !== 'boolean') errs.push(`${fw}: stop must be a boolean`); if (f.stop !== undefined && typeof f.stop !== 'boolean') errs.push(`${fw}: stop must be a boolean`);
if (f.uses !== undefined) {
if (!Array.isArray(f.uses)) errs.push(`${fw}: uses must be an array`);
else f.uses.forEach((u, m) => {
if (!isObj(u) || !isStr(u.record) || !u.record || !isStr(u.field) || !u.field) errs.push(`${fw}.uses[${m}]: needs record and field ids`);
});
}
}); });
} }

View File

@@ -217,6 +217,43 @@ export function addFrame(seq, image = '', layout = 'under') {
return f; return f;
} }
// ---- credential references from steps (issue #24) ----
// Every record in the document, wherever it sits (sequences are not records).
export function allRecordsIn(doc) {
return doc.sections.flatMap((s) => (s.records || []).filter((r) => !isSequence(r)));
}
// A field gets an id the first time something references it. Returns it.
export function ensureFieldId(record, field) {
if (field.id) return field.id;
const taken = new Set(record.fields.map((f) => f.id).filter(Boolean));
let n = record.fields.length + 1;
while (taken.has('f' + n)) n++;
field.id = 'f' + n;
return field.id;
}
// { record, field } for a reference, or null if either is gone or the field is an image.
export function findFieldRef(doc, ref) {
const record = allRecordsIn(doc).find((r) => r.id === ref.record);
const field = record && record.fields.find((f) => f.id === ref.field && f.kind !== 'image');
return field ? { record, field } : null;
}
// Add a reference to `frame` for `field` of `record`; no duplicates. Returns true if added.
export function addUse(frame, record, field) {
const ref = { record: record.id, field: ensureFieldId(record, field) };
frame.uses = frame.uses || [];
if (frame.uses.some((u) => u.record === ref.record && u.field === ref.field)) return false;
frame.uses.push(ref);
return true;
}
export function removeUse(frame, i) {
frame.uses.splice(i, 1);
if (!frame.uses.length) delete frame.uses;
}
// A step captured from the screen: a new frame, and the sequence remembers when, // A step captured from the screen: a new frame, and the sequence remembers when,
// so the reader can be told the site may have changed since. // so the reader can be told the site may have changed since.
export function addCapture(seq, image) { export function addCapture(seq, image) {

View File

@@ -58,6 +58,18 @@ export function frameView(frame, includeSecrets) {
}; };
} }
// What a step's credential references show: [{ label, record, value, secret, missing }].
// A secret value is a placeholder unless includeSecrets; a reference that no longer
// resolves is marked missing. Uses displayValue so masking matches the rest of print.
export function usesView(doc, frame, includeSecrets) {
return (frame.uses || []).map((u) => {
const rec = doc.sections.flatMap((s) => (s.records || []).filter((r) => !isSequence(r))).find((r) => r.id === u.record);
const f = rec && rec.fields.find((x) => x.id === u.field && x.kind !== 'image');
return f ? { label: f.label, record: rec.title, value: displayValue(f, includeSecrets), secret: !!f.secret, missing: false }
: { label: '', record: '', value: '', secret: false, missing: true };
});
}
export function countSecrets(doc) { export function countSecrets(doc) {
let n = 0; let n = 0;
const frames = (q) => { for (const f of q.frames) if (f.secret && (f.image || String(f.text || '').trim() || String(f.ifStuck || '').trim())) n++; }; const frames = (q) => { for (const f of q.frames) if (f.secret && (f.image || String(f.text || '').trim() || String(f.ifStuck || '').trim())) n++; };

View File

@@ -2,7 +2,7 @@
// YYMMDD is the date, X the iteration that day (A first, B second, ...). // YYMMDD is the date, X the iteration that day (A first, B second, ...).
// Bump with `npm run bump` (see bump-version.js). build.js strips `export`. // Bump with `npm run bump` (see bump-version.js). build.js strips `export`.
export const APP_VERSION = '261124B'; export const APP_VERSION = '261124C';
const VERSION_RE = /^(\d{6})([A-Z])$/; const VERSION_RE = /^(\d{6})([A-Z])$/;
export const isVersion = (v) => typeof v === 'string' && VERSION_RE.test(v); export const isVersion = (v) => typeof v === 'string' && VERSION_RE.test(v);

View File

@@ -362,3 +362,45 @@ test('addCapture adds a frame and stamps the sequence captured date; clearSecret
assert.equal(f.stop, true); assert.equal(f.stop, true);
assert.deepEqual(validate(doc), []); assert.deepEqual(validate(doc), []);
}); });
test('credential references: field ids are stable, refs resolve, missing ones are detected', async () => {
const { addUse, removeUse, findFieldRef, ensureFieldId, addSequence, addFrame } = await import('../src/ops.js');
const { usesView } = await import('../src/print.js');
const doc = emptyDocument('x');
const s = addSection(doc, 'records', 'Banking');
const r = addRecord(doc, s);
addField(r); addField(r);
r.fields[0].label = 'User'; r.fields[0].value = 'bob';
r.fields[1].label = 'Password'; r.fields[1].value = 'hunter2'; r.fields[1].secret = true;
const q = addSequence(doc, s);
const fr = addFrame(q);
assert.equal(addUse(fr, r, r.fields[1]), true);
assert.equal(addUse(fr, r, r.fields[1]), false); // no duplicates
addUse(fr, r, r.fields[0]);
assert.equal(ensureFieldId(r, r.fields[1]), r.fields[1].id); // stable
assert.notEqual(r.fields[0].id, r.fields[1].id);
assert.deepEqual(validate(doc), []);
const moved = r.fields.reverse(); // reorder: references still resolve
assert.equal(findFieldRef(doc, fr.uses[0]).field.label, 'Password');
assert.deepEqual(usesView(doc, fr, false).map((u) => [u.label, u.value]), [['Password', '[withheld]'], ['User', 'bob']]);
assert.equal(usesView(doc, fr, true)[0].value, 'hunter2');
r.fields.splice(0, 1); // delete the password field
assert.equal(findFieldRef(doc, fr.uses[0]), null);
assert.equal(usesView(doc, fr, true)[0].missing, true);
assert.deepEqual(validate(doc), []); // a dangling reference is not an error
removeUse(fr, 0); removeUse(fr, 0);
assert.equal(fr.uses, undefined);
});
test('validation: uses and field ids', async () => {
const { addSequence, addFrame } = await import('../src/ops.js');
const doc = emptyDocument('x');
const s = addSection(doc, 'records', 'B');
const r = addRecord(doc, s); addField(r); addField(r);
const q = addSequence(doc, s); const fr = addFrame(q);
const bad = (mut, re) => { const c = structuredClone(doc); mut(c.sections[0]); assert.match(validate(c).join('|'), re); };
bad((x) => { x.records[1].frames[0].uses = 'no'; }, /uses must be an array/);
bad((x) => { x.records[1].frames[0].uses = [{ record: 'a' }]; }, /needs record and field ids/);
bad((x) => { x.records[0].fields[0].id = 'f1'; x.records[0].fields[1].id = 'f1'; }, /duplicate field id/);
bad((x) => { x.records[0].fields[0].id = ''; }, /id must be a non-empty string/);
});