* Serve the legacy /web/*.js names from their .mjs siblings The build emitted <name>.js alongside <name>.mjs until the two compilations were collapsed into one. Dropping the second compilation was right, but it removed URLs the project itself had published: the v1.16.4 SPA documentation named /web/embed.js directly and its loader snippet requested .js. Pages that hard-coded those names now 404 with no deprecation. webFiles.Open retries a missing .js against the .mjs sibling. The bundles contain no import or export, so the same bytes serve both names. The retry runs only once both sources report the name missing, so a real .js still wins, and an unreadable sibling reports its own error rather than being flattened into the requested file's 404. Related to #2178 * Reuse only the comments iframe embed created createInstance took root.firstElementChild as its iframe, so anything a page left inside #remark42 was adopted instead. A <noscript> fallback became the "iframe", createIframe never ran, and the height messages went to an element that cannot show comments. That also defeats the placeholder support, which promises content in the root is cleared once the iframe reports inited: a text placeholder works, but any element placeholder is mistaken for the iframe, so inited never arrives and the cleanup never runs. The iframe now carries data-remark42-iframe and the lookup is scoped to a direct child, so a second createInstance still reuses it while nothing else in the root can be adopted. Related to #1990 * Assert the backup contents rather than the compressed size TestBackup_MakeBackup and TestBackup_Do pinned the gzip output at 52 bytes, which ties them to the exact output of compress/flate. The same input encodes to 57 bytes on go 1.27, so both fail for anyone building on a toolchain newer than the one CI pins. They now read the backup back and compare it against what the exporter wrote, which is what the tests were reaching for and does not move with the compressor. The payload is a shared constant so the two cannot drift.
101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package migrator
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
"testing/synctest"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBackup_RemoveOldBackupFiles(t *testing.T) {
|
|
loc := "/tmp/remark-backups.test"
|
|
defer os.RemoveAll(loc)
|
|
|
|
assert.NoError(t, os.MkdirAll(loc, 0o700))
|
|
|
|
for i := 1; i <= 10; i++ {
|
|
fname := fmt.Sprintf("%s/backup-site1-201712%02d.gz", loc, i)
|
|
err := os.WriteFile(fname, []byte("blah"), 0o600)
|
|
assert.NoError(t, err)
|
|
}
|
|
fname := fmt.Sprintf("%s/backup-site2-20171210.gz", loc)
|
|
err := os.WriteFile(fname, []byte("blah"), 0o600)
|
|
assert.NoError(t, err)
|
|
|
|
bk := AutoBackup{BackupLocation: loc, SiteID: "site1", KeepMax: 3}
|
|
bk.removeOldBackupFiles()
|
|
ff, err := os.ReadDir(loc)
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 4, len(ff), "should keep 4 files - 3 kept for sit1, and one for site2")
|
|
assert.Equal(t, "backup-site1-20171208.gz", ff[0].Name())
|
|
assert.Equal(t, "backup-site1-20171209.gz", ff[1].Name())
|
|
assert.Equal(t, "backup-site1-20171210.gz", ff[2].Name())
|
|
assert.Equal(t, "backup-site2-20171210.gz", ff[3].Name())
|
|
}
|
|
|
|
func TestBackup_MakeBackup(t *testing.T) {
|
|
loc := "/tmp/remark-backups.test"
|
|
defer os.RemoveAll(loc)
|
|
assert.NoError(t, os.MkdirAll(loc, 0o700))
|
|
|
|
bk := AutoBackup{BackupLocation: loc, SiteID: "site1", KeepMax: 3, Exporter: &mockExporter{}}
|
|
fname, err := bk.makeBackup()
|
|
assert.NoError(t, err)
|
|
expFile := fmt.Sprintf("/tmp/remark-backups.test/backup-site1-%s.gz", time.Now().Format("20060102"))
|
|
assert.Equal(t, expFile, fname)
|
|
|
|
assert.Equal(t, exportedPayload, gzContent(t, expFile))
|
|
}
|
|
|
|
func TestBackup_Do(t *testing.T) {
|
|
loc := "/tmp/remark-backups.test"
|
|
defer os.RemoveAll(loc)
|
|
assert.NoError(t, os.MkdirAll(loc, 0o700))
|
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
go func() {
|
|
time.Sleep(time.Second)
|
|
cancel()
|
|
}()
|
|
|
|
bk := AutoBackup{BackupLocation: loc, SiteID: "site1", KeepMax: 3, Exporter: &mockExporter{}, Duration: 600 * time.Millisecond}
|
|
bk.Do(ctx)
|
|
|
|
expFile := fmt.Sprintf("/tmp/remark-backups.test/backup-site1-%s.gz", time.Now().Format("20060102"))
|
|
assert.Equal(t, exportedPayload, gzContent(t, expFile))
|
|
})
|
|
}
|
|
|
|
const exportedPayload = "some export blah blah 1234567890"
|
|
|
|
// the compressed size is not assertable: it moves with the compress/flate version
|
|
func gzContent(t *testing.T, name string) string {
|
|
t.Helper()
|
|
fh, err := os.Open(name) //nolint:gosec // path is built by the test
|
|
require.NoError(t, err)
|
|
defer func() { assert.NoError(t, fh.Close()) }()
|
|
|
|
gz, err := gzip.NewReader(fh)
|
|
require.NoError(t, err)
|
|
defer func() { assert.NoError(t, gz.Close()) }()
|
|
|
|
b, err := io.ReadAll(gz)
|
|
require.NoError(t, err)
|
|
return string(b)
|
|
}
|
|
|
|
type mockExporter struct{}
|
|
|
|
func (mock *mockExporter) Export(w io.Writer, _ string) (int, error) {
|
|
_, err := w.Write([]byte(exportedPayload))
|
|
return 1000, err
|
|
}
|