Files
Roger a734e777a8 refactor(app): export one function per command (#712)
* refactor(app): export one function per command

Step 2 of #710. A command's logic was split between cmd and stage-shaped app calls; cmd now parses flags, calls one function and prints what it returns. An error means the command never ran, and a run that ended early says so in Summary.Err.

* fix(app): check dumpkeys and archive -o up front

A missing output directory was reported only after the keys were retrieved or the files copied.

* docs(rfcs): move restore's input out of the command layer

Restore now discovers its directories in an archive itself, so the command layer no longer holds the data it reads.
2026-11-22 14:45:39 +08:00

68 lines
2.0 KiB
Go

package snapshot
import (
"archive/zip"
"bytes"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/zipfile"
)
func TestZip_RoundTrip(t *testing.T) {
files := map[string][]byte{
"small.txt": []byte("hello"),
"chrome/Default/Network/Cookies": []byte("cookie-bytes"),
}
snapshotRoot := t.TempDir()
for relativePath, data := range files {
writeFile(t, filepath.Join(snapshotRoot, filepath.FromSlash(relativePath)), data)
}
archive := filepath.Join(t.TempDir(), "out.zip")
require.NoError(t, Open(snapshotRoot).Zip(archive))
zipReader, err := zip.OpenReader(archive)
require.NoError(t, err)
for _, zipFile := range zipReader.File {
assert.NotContains(t, zipFile.Name, `\`, "zip entry names are forward-slash")
}
require.NoError(t, zipReader.Close())
unzipped := t.TempDir()
require.NoError(t, zipfile.Extract(archive, unzipped))
for relativePath, want := range files {
got, err := os.ReadFile(filepath.Join(unzipped, filepath.FromSlash(relativePath)))
require.NoError(t, err, relativePath)
assert.True(t, bytes.Equal(want, got), "%s: got %d bytes, want %d", relativePath, len(got), len(want))
}
}
func TestZip_MissingSnapshotDirFails(t *testing.T) {
err := Open(filepath.Join(t.TempDir(), "missing")).Zip(filepath.Join(t.TempDir(), "browser-data.zip"))
require.Error(t, err)
}
func snapshotFixture(t *testing.T) string {
t.Helper()
root := t.TempDir()
writeFile(t, filepath.Join(root, "chrome", "Local State"), []byte("{}"))
return root
}
func TestZip_ReplacesAnExistingArchive(t *testing.T) {
archive := filepath.Join(t.TempDir(), "browser-data.zip")
writeFile(t, archive, []byte("stale"))
require.NoError(t, Open(snapshotFixture(t)).Zip(archive))
zipReader, err := zip.OpenReader(archive)
require.NoError(t, err)
defer zipReader.Close()
require.Len(t, zipReader.File, 1)
assert.Equal(t, "chrome/Local State", zipReader.File[0].Name)
}