Skip to content

Commit c2bc538

Browse files
kckernclaude
andcommitted
fix(web): "0" is an empty reference, as PHP's empty() made it
/0 rendered home on live (empty($ref)), and cleanReference returned false, so meta/api/plain gave the empty block. Node looked up verse 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1e982fc commit c2bc538

6 files changed

Lines changed: 23 additions & 7 deletions

File tree

‎apps/web/src/app/controllers/ScriptureController.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import searchParallelController from './SearchParallelController.js';
33
import outputMiddleware from '../middleware/output.js';
44
import referenceService from '../../services/ReferenceService.js';
55
import { bookOnlyQuery } from '../../domain/rules/referenceParsing.js';
6+
import { isEmptyReference } from '../../domain/rules/requestParsing.js';
67

78
// Formats whose reference blocks ("meta") name a book given alone (scriptures::loadMetaFromRef)
89
const BOOK_ONLY_FORMATS = new Set(['meta', 'api']);
@@ -35,9 +36,9 @@ export class ScriptureController {
3536
// A parallel q reached through ?q= has no route param: use the parsed q.
3637
const reference = config.parallel ? (req.params?.reference || config.reference) : (config.reference ?? req.params.reference);
3738

38-
if (!reference && !config.parallel && !answersEmpty(config)) return this.home(req, res);
39+
if (isEmptyReference(reference) && !config.parallel && !answersEmpty(config)) return this.home(req, res);
3940

40-
const result = await this.lookupService.lookup(reference, config);
41+
const result = await this.lookupService.lookup(isEmptyReference(reference) ? '' : reference, config);
4142
result.bookOnly = await this.bookOnly(result, config);
4243

4344
// Attach result for output middleware

‎apps/web/src/app/controllers/SearchParallelController.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import parallelService from '../../services/ParallelService.js';
22
import searchService from '../../services/SearchService.js';
33
import { slugifyRef } from '../../domain/rules/legacyReference.js';
44
import { OutputFormat } from '../../domain/enums/OutputFormat.js';
5-
import { searchVersion } from '../../domain/rules/requestParsing.js';
5+
import { searchVersion, isEmptyReference } from '../../domain/rules/requestParsing.js';
66

77
// The html body of the PHP front controller (index.php), in its order, from the parsed request
88
// (req.config), never from the Express route params:
@@ -29,7 +29,7 @@ export class SearchParallelController {
2929
return true;
3030
}
3131

32-
if (!config.reference) {
32+
if (isEmptyReference(config.reference)) {
3333
res.render('home', { config });
3434
return true;
3535
}

‎apps/web/src/domain/rules/requestParsing.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export function parseVersionSpec(spec, { keepEmpty = false } = {}) {
7676
return list;
7777
}
7878

79+
// PHP's empty() on a reference string: "" and "0" are both empty (index.php's empty($ref) → home,
80+
// cleanReference → false).
81+
export const isEmptyReference = ref => ref == null || ref === '' || ref === '0';
82+
7983
// The version a search is limited to: PHP's $forcever, the format of the last piece of the
8084
// version spec ("kjv.1830*rebom" → "REBOM"); null without a version spec or for an empty piece.
8185
export function searchVersion(versionSpec) {

‎apps/web/src/presentation/legacy/meta.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { lookupReference } from 'scripture-guide';
44
import { defaultVersions as defaultVersionList, versionOrder } from '../../domain/rules/defaultVersions.js';
55
import { getBook } from '../../domain/rules/legacyReference.js';
6+
import { isEmptyReference } from '../../domain/rules/requestParsing.js';
67
import { bookOnlyQuery } from '../../domain/rules/referenceParsing.js';
78

89
export { getBook, bookOnlyQuery };
@@ -68,9 +69,10 @@ export function bookOnlyBlock(bookTitle, verseIds) {
6869

6970
// [{orig, book, verse_ids}] per ";"-separated block. A block after the first keeps the leading
7071
// space left by the split, so PHP prefixed the book to it: "Genesis Genesis 1:1". Reproduced.
71-
// An empty reference is one block whose verse_ids is false (cleanReference returned false).
72+
// An empty reference ("" or "0", PHP empty()) is one block whose verse_ids is false
73+
// (cleanReference returned false).
7274
export function metaFromRef(ref) {
73-
if (ref === '') return [{ orig: '', book: '', verse_ids: false }];
75+
if (isEmptyReference(ref)) return [{ orig: '', book: '', verse_ids: false }];
7476
const normalized = normalize(ref).replace(/—/g, ' ');
7577
if (!normalized) return [];
7678
const blocks = [];

‎apps/web/test/unit/emptyReference.test.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ describe('an empty reference on a non-HTML host', () => {
4040
});
4141
});
4242

43+
// PHP's empty() is true for "0", so /0 is an empty reference too.
44+
describe('"0" is an empty reference (PHP empty)', () => {
45+
test('meta and api give the empty block, plain the not-found text', async () => {
46+
expect(JSON.parse((await get('meta.scripture.guide', '/0')).body)).toEqual([{ orig: '', book: '', verse_ids: false }]);
47+
expect(JSON.parse((await get('api.scripture.guide', '/0')).body).meta).toEqual([{ orig: '', book: '', verse_ids: false, version: '' }]);
48+
expect((await get('plain.scripture.guide', '/0')).body).toBe('No scripture verses found.');
49+
});
50+
});
51+
4352
describe('a parallel ?q= on a non-HTML host', () => {
4453
test('api answers 200 with an empty body, as live', async () => {
4554
const res = await get('api.scripture.guide', '/?q=john.3.16~gen.1.1');

‎apps/web/test/unit/searchParallelController.test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('SearchParallelController.dispatch', () => {
7171

7272
test('an empty reference renders home, before search', async () => {
7373
const { ctl, search, res, req } = setup();
74-
for (const path of ['/~/', '/__debug__/', '//feed/', '/x/', '/john.3.16']) {
74+
for (const path of ['/~/', '/__debug__/', '//feed/', '/x/', '/0', '/kjv/0', '/john.3.16']) {
7575
res.render.mockClear();
7676
const query = path === '/john.3.16' ? { q: '' } : {};
7777
expect(await ctl.dispatch(req(path, { query }), res)).toBe(true);

0 commit comments

Comments
 (0)