Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

field classes: a tier's {set} mode cell fires even when the source lacks the field (#666)

nyblnet9c8561803e49
diff --git a/kernel/src/docfields.ts b/kernel/src/docfields.ts
index eac71f31..14d06113 100644
--- a/kernel/src/docfields.ts
+++ b/kernel/src/docfields.ts
@@ -281,6 +281,15 @@ export function projectForCopy<T extends object>(
: (CLASS_RULES[cls][tier] as Rule)
applyRule(out, key, v, rule, opts)
}
+ // A {set} mode cell is a property of the TIER, not of the source: a reader copy
+ // is read-only and a template copy is a template whether or not the source
+ // carried the field at all. The walk above only visits the source's keys, and
+ // most documents have neither readonly nor template — so without this pass a
+ // doc with no `readonly` key produced an EDITABLE reader copy (fail-open).
+ for (const field of Object.keys(MODE_FIELD_RULES) as ModeField[]) {
+ const rule = MODE_FIELD_RULES[field][tier] as Rule
+ if (typeof rule === 'object' && 'set' in rule) out[field] = rule.set
+ }
return out as T
}
diff --git a/scripts/test-field-class.ts b/scripts/test-field-class.ts
index 6ed69f04..92e90cfe 100644
--- a/scripts/test-field-class.ts
+++ b/scripts/test-field-class.ts
@@ -144,11 +144,32 @@ console.log('\nthe unknown-key rule: file + duplicate only')
}
}
-console.log('\nprojectForCopy builds from empty — a field absent from the live doc is never invented')
+console.log('\nprojectForCopy builds from empty — nothing is invented except a tier\'s {set} mode cell')
{
const bare = { docId: 'D', title: 'only' }
const out = projectForCopy(bare, MAP, 'reader', OPTS)
- ok(same(keysOf(out), ['docId', 'title']), 'only the present fields appear; no readonly/collab fabricated')
+ ok(same(keysOf(out), ['docId', 'readonly', 'title']),
+ 'only the present fields appear, plus readonly (the reader tier sets it) — no collab fabricated')
+}
+
+console.log('\na {set} mode cell fires even when the SOURCE has no such field')
+{
+ // Most real documents carry neither readonly nor template. The set cells are
+ // a property of the tier, so they must not depend on the source having the
+ // key — a doc with no readonly must still yield a READ-ONLY reader copy.
+ const noModes = () => { const d = liveDoc() as Record<string, unknown>; delete d.readonly; delete d.template; return d }
+ for (const tier of ['reader', 'audience', 'package', 'link'] as Tier[]) {
+ const out = projectForCopy(noModes(), MAP, tier, OPTS) as Record<string, unknown>
+ ok(out.readonly === true, `${tier} copy of a doc with NO readonly key is still read-only`)
+ ok(!('template' in out), `${tier} copy invents no template`)
+ }
+ const tpl = projectForCopy(noModes(), MAP, 'template', OPTS) as Record<string, unknown>
+ ok(tpl.template === true, 'template copy of a doc with NO template key is still a template')
+ ok(!('readonly' in tpl), 'and invents no readonly')
+ for (const tier of ['file', 'duplicate', 'invite', 'copyJSON'] as Tier[]) {
+ const out = projectForCopy(noModes(), MAP, tier, OPTS) as Record<string, unknown>
+ ok(!('readonly' in out) && !('template' in out), `${tier} invents no mode field (no set cell there)`)
+ }
}
console.log('\ndocContentKey: counts content + history, excludes capability + bookkeeping, skips absent')