53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package runtime
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildJSDeployZipExportsMainAndHandler(t *testing.T) {
|
|
zipBytes, err := BuildJSDeployZip(`module.exports = async function () { return { status: 200, body: "ok" }; }`)
|
|
if err != nil {
|
|
t.Fatalf("BuildJSDeployZip() error = %v", err)
|
|
}
|
|
|
|
zr, err := zip.NewReader(bytes.NewReader(zipBytes), int64(len(zipBytes)))
|
|
if err != nil {
|
|
t.Fatalf("zip.NewReader() error = %v", err)
|
|
}
|
|
|
|
files := map[string]string{}
|
|
for _, file := range zr.File {
|
|
rc, openErr := file.Open()
|
|
if openErr != nil {
|
|
t.Fatalf("open %q: %v", file.Name, openErr)
|
|
}
|
|
content, readErr := io.ReadAll(rc)
|
|
_ = rc.Close()
|
|
if readErr != nil {
|
|
t.Fatalf("read %q: %v", file.Name, readErr)
|
|
}
|
|
files[file.Name] = string(content)
|
|
}
|
|
|
|
mainJS := files["main.js"]
|
|
if mainJS == "" {
|
|
t.Fatalf("main.js not found in zip, files: %v", files)
|
|
}
|
|
if !strings.Contains(mainJS, `new Function('module', 'exports', 'require',`) {
|
|
t.Fatalf("main.js does not pass require to new Function: %s", mainJS)
|
|
}
|
|
if !strings.Contains(mainJS, `__mod.exports, require)`) {
|
|
t.Fatalf("main.js does not forward require: %s", mainJS)
|
|
}
|
|
if !strings.Contains(mainJS, `module.exports = __invoke`) {
|
|
t.Fatalf("main.js does not export __invoke via module.exports: %s", mainJS)
|
|
}
|
|
if !strings.Contains(mainJS, `_fn.default || _fn.handler || _fn.main`) {
|
|
t.Fatalf("main.js lost user export resolution: %s", mainJS)
|
|
}
|
|
}
|