LinkUnzip is a Chrome extension that opens a zip from its link and lets you preview or save single files without downloading the whole zip. Under the hood it's four pieces talking through messages: the popup, a background service worker, an offscreen page that does the downloading and unzipping, and a viewer tab for previews.
My end-to-end tests clicked through the real extension against real servers and passed, 39 out of 39. They test what I thought of. What I wanted to find was the stuff in between: a message that shows up a little too late, a worker that Chrome shuts down at a bad moment, a double click.
So I drew the whole app as state machines. It found 10 bugs that the tests never touched.
The method
1. List everything that has a lifetime
Anything that starts, waits and ends gets its own diagram. For LinkUnzip that came to about 20:
- a job (read the zip's index, wait for the user, extract, done)
- the connection to the offscreen page, and the one to the optional Windows helper
- a question sent to the offscreen page and waiting for its reply
- one file being saved into Downloads
- the preview page, one thumbnail in the image grid, a preview token
The small ones count. "One file being saved" alone has seven states, because Chrome's downloads API can stall without a name, get flagged as dangerous, or be cancelled by the user.
2. Draw each one from the code
Read the code and write down every place that changes the state. If a function does job.phase = "extracting", that's an arrow. Here's the job lifecycle, written in Mermaid so the diagram lives next to the code as text:
stateDiagram-v2 [*] --> inspecting : link, menu, share link, inner zip inspecting --> ready : inspected inspecting --> error : error ready --> extracting : extract extracting --> done : done extracting --> cancelled : cancelled extracting --> error : error error --> extracting : retry, resume, password cancelled --> extracting : resume Draw it from the code. If you draw from memory you get the design, and the bugs are in the places where the code does something else.
3. Ask every state about every event
This is the step that finds bugs. Take each state, take each message or event that could possibly arrive, and check in the code what happens. A "done" while the job is still inspecting? An error while it's already done? A second click on Start while it's extracting?
Most combinations are fine or impossible. The ones where you have to stop and think are where the bugs are.
4. Treat every await as a state
In async JavaScript, anything can happen while you await. The user clicks something else, the worker restarts, another reply comes in. If code reads some state, awaits, and then acts on what it read, that gap is a state of its own and needs an arrow for "the world changed meanwhile".
What it found
A finished job reported as failed
Chrome stops an extension's service worker when it's idle and starts it again on the next event. On startup my worker runs restore() to load the saved jobs back from storage. Messages from the offscreen page went straight to the handler, even if restore() hadn't finished.
So if an extraction finished right while the worker was waking up, the "done" message arrived, found no job, and got dropped. Ten seconds later a check saw a job that was "extracting" with no sign of life and marked it as interrupted. The files were all there, and the popup said it failed.
The diagram made this obvious: "Loading" was a state, and nothing said what happens to messages in it. The fix is one line, wait for the load before handling replies:
ready.then(() => onReply(msg)); Cancel that does nothing
The offscreen page keeps a map of running tasks, so Cancel can find and stop them. Each task removed itself in a finally when it ended. But two index reads can legitimately run under the same id, and the first one to finish deleted the entry of the other. After that, Cancel had nothing to stop.
Only delete your own entry:
if (running.get(id) === ctl) running.delete(id); This is a pattern worth looking for anywhere: cleanup code that assumes it's the only owner.
A late error turning a done job red
Replies from the offscreen page were applied to the job whatever phase it was in. An error from an earlier index read could arrive after the user had already moved on, and push a job that was ready, or even done, into the error state. One guard fixes it: messages about an extraction count only while the job is extracting, and errors about the index only while it's being read.
The preview showing the previous file
In the viewer, flipping quickly through files with the arrow keys could draw the wrong one. The code asked for file A, awaited, and drew the result, even if the user had moved on to file B in the meantime. A slow reply for A would land on top of B. That's step 4 exactly: the await was a state with no exit for "user moved on". The fix checks after the await that the request is still the current one, and if it isn't, draws nothing.
The rest
A double click on Start could begin two extractions of the same job. Cancel during the index read forgot the job but left the read running. A redraw check ignored one field, so a button didn't appear until you reopened the popup. And a couple more of the same kind. All of them need
bad timing, which is why the tests never hit them.
Doing it better
Some things I'd do differently next time:
- Start with the parts that talk to each other. Most of my bugs were at the edges, where one piece sends a message and assumes the other is in a certain state. Draw the connections and the question/reply flow first.
- Keep the fixes small. I set myself a rule: change an existing line or add a guard next to one. No new helpers, no new message types. Every fix above is one to five lines, which made them easy to review and hard to get wrong.
- Write down what you leave out. Two problems would have needed a lot of code for a case that almost never happens. I wrote down why I skipped them, so I don't rediscover them in six months and wonder.
- Test what you can automatically and the rest by hand. Two of the fixes got automated tests. The worker restart ones I tested by stopping the worker from
chrome://serviceworker-internalsin the middle of a big extraction. - Let an AI draw, then check every arrow yourself. I did this in a chat with Claude. It read the code and drew the first versions of the diagrams fast, which is the boring part. Then I went through each arrow against the code, and that's where I found out which arrows were wrong and which states had no arrows at all.
All ten fixes ship in LinkUnzip 1.4.2.
If you build anything with a service worker, a WebSocket or a queue, try drawing just one of its connections this way. I'd like to know if it finds something for you too.
