Skip to content

test: cover the day cache and cli dispatch directly - #48

Merged
only-cli merged 1 commit into
mainfrom
test/cache-and-cli-dispatch
Sep 2, 2026
Merged

test: cover the day cache and cli dispatch directly#48
only-cli merged 1 commit into
mainfrom
test/cache-and-cli-dispatch

Conversation

@only-cli

@only-cli only-cli commented Sep 2, 2026

Copy link
Copy Markdown
Owner

First step on test coverage: the two modules with the thinnest direct coverage. src/cache.js had no test importing it, and src/cli.js was exercised only through its auth commands in tests/cli-auth.test.js.

tests/cache.test.js (7 tests)

The network is a local HTTP proxy that records every request, reached through HTTP_PROXY the same way the fetch tests do, so "the network was never asked" is asserted rather than assumed. Nothing leaves the machine.

  • A miss fetches, parses, and writes the file under <kind>/<host><ext>.
  • A fresh file is served from disk and the proxy sees zero requests.
  • A file older than a day is refetched and replaced.
  • A body the parser rejects is never written, so a block page cannot poison the cache.
  • A stale good copy survives a refetch whose body the parser rejects.
  • A cache directory that cannot be created costs only the refetch, twice in a row.
  • A URL with no extension caches under the bare host, and kinds do not share directories.

tests/cli.test.js (18 tests)

Each case spawns the real binary against a throwaway OC_HOME. The page under test is seeded straight into a session file, so read, next, find, and do on text all have something to answer with, and no test fetches.

  • No command, --help, and -h print the usage and exit 0.
  • The usage names every dispatchable command exactly once, so the help text and the dispatch table cannot drift apart silently.
  • An unknown first word fails in one line pointing at --help.
  • A site name is tried as a shortcut before it is called unknown, so a wrong verb reports the site's verbs.
  • --budget must be a positive number and --session must be a safe filename, both checked before any command runs.
  • read, next, find, and do with nothing open all say to run open first.
  • open and raw with no URL print a usage line.
  • read <n> prints the region, and a bad number gives usage rather than a stack trace.
  • next reports the end of a page that fit in one render.
  • find joins the rest of argv into one query, and fails with usage on none.
  • do on a text number reads it in place; a recording proxy proves no request left.
  • do with no number, or a number the page lacks, fails in one line.
  • fill, submit, back, and session fail with the same one-line message naming themselves.
  • sites lists the bundled shortcuts.
  • Flags work before or after the command.

Verification

Every test was checked against a deliberate break in the code it guards, each caught by the test meant to catch it:

Break Caught by
TTL check removed fresh file served from disk
write moved before parse both poison tests
--budget check removed budget validation
shortcut lookup removed site name before unknown
fill wired to submit planned commands name themselves
find joins argv with '' find joins argv

Full suite: 212 passing (was 188), offline, no new dependencies.

cache.js had no test importing it, and cli.js was tested only through its
auth commands. Both now have a file of their own.

tests/cache.test.js proves the policy the module's comment states: a miss
fetches, parses, and writes under host and extension; a fresh copy is served
from disk without touching the network; a copy older than a day is refetched
and replaced; a body the parser rejects is never written, and a stale good
copy survives such a refetch; a cache directory that cannot be created costs
only the refetch. The network is a local proxy that records what it sees, so
"never asked" is asserted, not assumed.

tests/cli.test.js spawns the real binary against a throwaway OC_HOME with a
page seeded straight into a session file, so read, next, find, and do on
text all have something to answer with and nothing fetches. It covers help
on no command and both help flags, the help text naming every dispatchable
command once, unknown first words, a site name reaching the shortcut
resolver before being called unknown, budget and session name validation
running before any command, the nothing-open message from every session
command, the usage lines of open, raw, read, find, and do, do on a text
number reading in place with a recording proxy proving no request left, the
four planned commands failing with one line that names themselves, sites,
and flags placed before or after the command.

Each test was checked against a deliberate break in the code it guards.
@only-cli
only-cli force-pushed the test/cache-and-cli-dispatch branch from 0a43456 to 5657d60 Compare September 2, 2026 14:38
@only-cli
only-cli merged commit 10bf7b3 into main Sep 2, 2026
4 checks passed
@only-cli
only-cli deleted the test/cache-and-cli-dispatch branch September 2, 2026 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant