Files
Benjamin Liu 7e44e71150 fix(lesswrong): drop "Unknown" silent sentinel in author column (#1611)
* fix(lesswrong): drop "Unknown" silent sentinel in author column

Twelve lesswrong commands had `author: item.user?.displayName ?? 'Unknown'`
which masks the missing-author signal: an agent reading the result row
cannot distinguish "post has no associated user" from "author is literally
named Unknown". The repo's typed-error lint flags this pattern
(silent-sentinel rule, see scripts/check-typed-error-lint.mjs:323).

Replace `?? 'Unknown'` with `?? ''` so the missing-author case stays
visible as an empty string. Consistent with `clis/lesswrong/_helpers.js:68`
which was already using the empty-signal form.

Shrinks scripts/typed-error-lint-baseline.json from 173 to 161 entries.

Follows the same direction as #1603 (fix(adapters): surface silent empty
fallbacks).

Verified live: `opencli lesswrong frontpage --limit 2 -f json` returns
real posts with non-empty author values; empty-author rows would now
show `"author": ""` instead of fabricating `"Unknown"`.

* test(lesswrong): add empty-signal coverage for the author sentinel swap

Per owner's pattern in 71646158 (douyin/user-videos.test.js +
jike/read.test.js + weread/search-regression.test.js), pairs the
silent-sentinel value swap in this PR with a focused unit test that
mocks the upstream LessWrong GraphQL response to return posts where
`user` is null or `user.displayName` is missing, and asserts the row
surfaces `author: ''` instead of the old fabricated `'Unknown'`.

`clis/lesswrong/frontpage.test.js` is representative for the twelve
identical `author: item.user?.displayName ?? ''` swaps across
comments / curated / frontpage / new / read / sequences / shortform /
tag / top / top-month / top-week / top-year, all of which share the
exact same expression with no downstream sentinel consumer.

The empty-signal path is exercised live too: a deleted-account or
permission-restricted user shows up in the GraphQL response with
`user: null`, surfaces as `author: ''` post this PR (was 'Unknown'
before).
2026-05-18 19:18:51 +08:00

66 lines
2.3 KiB
JavaScript

import { cli, Strategy } from '@jackwener/opencli/registry';
import { EmptyResultError } from '@jackwener/opencli/errors';
import { DOMAIN, SITE, gqlEscape, gqlRequest, parsePostId, stripHtml, } from './_helpers.js';
cli({
site: SITE,
name: 'comments',
access: 'read',
description: 'Top comments on a post',
domain: DOMAIN,
strategy: Strategy.PUBLIC,
browser: false,
args: [
{
name: 'url-or-id',
type: 'string',
required: true,
positional: true,
help: 'Post URL or LessWrong post ID',
},
{ name: 'limit', type: 'int', default: 5, help: 'Number of comments' },
],
columns: ['rank', 'score', 'author', 'text'],
func: async (kwargs) => {
const postId = gqlEscape(parsePostId(String(kwargs['url-or-id'])));
const limit = Number(kwargs.limit ?? 5);
// Fetch post title and comments in parallel
const [postData, commentsData] = await Promise.all([
gqlRequest(`query PostTitle {
post(input: {selector: {documentId: "${postId}"}}) {
result { _id title slug }
}
}`),
gqlRequest(`query Comments {
comments(input: {terms: {view: "postCommentsTop", postId: "${postId}", limit: ${limit}}}) {
results { _id user { displayName } baseScore htmlBody postedAt }
}
}`),
]);
const post = postData?.post?.result;
if (!post?._id) {
throw new EmptyResultError('lesswrong comments', `Post "${postId}" not found`);
}
const comments = (commentsData?.comments?.results ?? []);
const rows = [];
// First row: post context
rows.push({
rank: '',
score: '',
author: '',
text: `Comments on: ${post.title ?? 'Untitled'} (https://${DOMAIN}/posts/${post._id}/${post.slug})`,
});
for (let i = 0; i < comments.length; i++) {
const item = comments[i];
const user = item.user;
const raw = stripHtml(item.htmlBody ?? '');
rows.push({
rank: i + 1,
score: item.baseScore ?? 0,
author: user?.displayName ?? '',
text: raw.length > 500 ? `${raw.slice(0, 500)}...` : raw,
});
}
return rows;
},
});