This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
{ system, self, pkgs, ... }:
|
||||
pkgs.lib.optionalAttrs (system == "x86_64-linux") {
|
||||
affine-runtime = pkgs.runCommand "affine-runtime" {
|
||||
nativeBuildInputs = [
|
||||
pkgs.dash
|
||||
pkgs.curl
|
||||
pkgs.jq
|
||||
pkgs.nodejs_22
|
||||
(pkgs.postgresql_16.withPackages (ps: [ ps.pgvector ]))
|
||||
pkgs.redis
|
||||
];
|
||||
AFFINE_PACKAGE = self.packages.${system}.affine;
|
||||
AFFINE_CLIENT_TEST = ./test/client.cjs;
|
||||
} ''
|
||||
dash ${./test/runtime.sh}
|
||||
touch "$out"
|
||||
'';
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const { randomBytes } = require('node:crypto');
|
||||
|
||||
const base = 'http://127.0.0.1:13011';
|
||||
const password = randomBytes(18).toString('base64url');
|
||||
let cookies = '';
|
||||
|
||||
async function json(path, data, authenticated = true) {
|
||||
const headers = { 'Content-Type': 'application/json' };
|
||||
if (authenticated && cookies) {
|
||||
headers.Cookie = cookies;
|
||||
const csrf = cookies.split('; ').find(cookie => cookie.startsWith('affine_csrf_token='));
|
||||
if (csrf) headers['x-affine-csrf-token'] = decodeURIComponent(csrf.slice(csrf.indexOf('=') + 1));
|
||||
}
|
||||
return fetch(`${base}${path}`, { method: 'POST', headers, body: JSON.stringify(data) });
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const setup = await json('/api/setup/create-admin-user', {
|
||||
name: 'Native test', email: 'native-test@example.invalid', password,
|
||||
}, false);
|
||||
assert.equal(setup.status, 201, 'First administrator setup');
|
||||
cookies = setup.headers.getSetCookie().map(cookie => cookie.split(';', 1)[0]).join('; ');
|
||||
assert(cookies.includes('affine_session='), 'Setup must issue a session');
|
||||
|
||||
const create = await json('/graphql', { query: 'mutation { createWorkspace { id } }' });
|
||||
assert.equal(create.status, 200, 'Workspace creation HTTP status');
|
||||
const created = await create.json();
|
||||
assert(!created.errors, 'Workspace creation must succeed');
|
||||
const id = created.data.createWorkspace.id;
|
||||
|
||||
const list = await json('/graphql', { query: 'query { workspaces { id } }' });
|
||||
const listed = await list.json();
|
||||
assert(!listed.errors, 'Workspace listing must succeed');
|
||||
assert(listed.data.workspaces.some(workspace => workspace.id === id), 'Created workspace must persist');
|
||||
|
||||
const anonymous = await json('/graphql', { query: 'query { workspaces { id } }' }, false);
|
||||
const anonymousData = await anonymous.json();
|
||||
assert(anonymous.status >= 400 || anonymousData.errors, 'Private workspaces must not be public');
|
||||
|
||||
const signup = await json('/api/auth/sign-in', { email: 'unknown@example.invalid' }, false);
|
||||
assert.equal(signup.status, 403, 'Public signup must remain disabled');
|
||||
const takeover = await json('/api/setup/create-admin-user', {}, false);
|
||||
assert.equal(takeover.status, 403, 'First administrator setup must close');
|
||||
|
||||
const login = await json('/api/auth/sign-in', { email: 'native-test@example.invalid', password }, false);
|
||||
assert.equal(login.status, 200, 'Existing administrator login');
|
||||
console.log('Administrator setup, authentication, private workspace CRUD and signup protection verified');
|
||||
}
|
||||
|
||||
main().catch(error => { console.error(error.message); process.exitCode = 1; });
|
||||
@@ -0,0 +1,106 @@
|
||||
#!/bin/dash
|
||||
set -eu
|
||||
|
||||
export HOME="$TMPDIR/affine-home"
|
||||
export DATABASE_URL="postgresql://affine@127.0.0.1:15432/affine"
|
||||
export REDIS_SERVER_HOST=127.0.0.1
|
||||
export REDIS_SERVER_PORT=16388
|
||||
export AFFINE_SERVER_PORT=13011
|
||||
export PORT=13011
|
||||
export LISTEN_ADDR=127.0.0.1
|
||||
export AFFINE_INDEXER_ENABLED=false
|
||||
export NODE_OPTIONS=--max-old-space-size=2048
|
||||
|
||||
mkdir -p "$HOME/.affine/config" "$HOME/.affine/storage" "$TMPDIR/pgsocket"
|
||||
cat > "$HOME/.affine/config/config.json" <<'JSON'
|
||||
{
|
||||
"server": {
|
||||
"name": "AFFiNE native test",
|
||||
"externalUrl": "http://127.0.0.1:13011",
|
||||
"listenAddr": "127.0.0.1",
|
||||
"port": 13011
|
||||
},
|
||||
"auth": { "allowSignup": false, "allowSignupForOauth": false },
|
||||
"copilot": { "enabled": false }
|
||||
}
|
||||
JSON
|
||||
|
||||
app_pid=
|
||||
redis_pid=
|
||||
cleanup() {
|
||||
if [ -n "$app_pid" ]; then kill "$app_pid" 2>/dev/null || true; fi
|
||||
if [ -n "$redis_pid" ]; then kill "$redis_pid" 2>/dev/null || true; fi
|
||||
pg_ctl -D "$TMPDIR/database" -m immediate stop >/dev/null 2>&1 || true
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
initdb -D "$TMPDIR/database" --username=postgres --auth=trust --no-locale --encoding=UTF8 > "$TMPDIR/initdb.log"
|
||||
pg_ctl -D "$TMPDIR/database" -l "$TMPDIR/postgresql.log" \
|
||||
-o "-h 127.0.0.1 -p 15432 -k $TMPDIR/pgsocket" start
|
||||
createuser --host=127.0.0.1 --port=15432 --username=postgres affine
|
||||
createdb --host=127.0.0.1 --port=15432 --username=postgres --owner=affine affine
|
||||
psql --host=127.0.0.1 --port=15432 --username=postgres --dbname=affine \
|
||||
--set=ON_ERROR_STOP=1 --command='CREATE EXTENSION vector;'
|
||||
redis-server --bind 127.0.0.1 --port 16388 --save '' --appendonly no \
|
||||
> "$TMPDIR/redis.log" 2>&1 &
|
||||
redis_pid=$!
|
||||
for _attempt in $(seq 1 30); do
|
||||
if redis-cli -h 127.0.0.1 -p 16388 ping >/dev/null 2>&1; then break; fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
if ! "$AFFINE_PACKAGE/bin/affine-migrate" > "$TMPDIR/migrate.log" 2>&1; then
|
||||
cat "$TMPDIR/migrate.log"
|
||||
exit 1
|
||||
fi
|
||||
test -s "$HOME/.affine/config/private.key"
|
||||
test "$(stat -c %a "$HOME/.affine/config/private.key")" = 600
|
||||
cp "$HOME/.affine/config/private.key" "$TMPDIR/original-private.key"
|
||||
|
||||
"$AFFINE_PACKAGE/bin/affine-server" > "$TMPDIR/server.log" 2>&1 &
|
||||
app_pid=$!
|
||||
ready=false
|
||||
for _attempt in $(seq 1 90); do
|
||||
if curl --fail --silent http://127.0.0.1:13011/info > "$TMPDIR/info.json"; then
|
||||
ready=true
|
||||
break
|
||||
fi
|
||||
if ! kill -0 "$app_pid" 2>/dev/null; then break; fi
|
||||
sleep 1
|
||||
done
|
||||
if [ "$ready" != true ]; then
|
||||
cat "$TMPDIR/server.log"
|
||||
exit 1
|
||||
fi
|
||||
if ! jq -e '.type == "selfhosted" and .compatibility == "0.27.4"' "$TMPDIR/info.json"; then
|
||||
cat "$TMPDIR/info.json"
|
||||
exit 1
|
||||
fi
|
||||
curl --fail --silent http://127.0.0.1:13011/admin/setup > "$TMPDIR/setup.html"
|
||||
jq -R -s -e 'contains("<html")' "$TMPDIR/setup.html"
|
||||
|
||||
# Match every script/style reference against the final immutable installation.
|
||||
node - "$AFFINE_PACKAGE/share/affine" <<'JS'
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const app = process.argv[2];
|
||||
for (const subdir of ['', 'admin', 'mobile']) {
|
||||
const dir = path.join(app, 'static', subdir);
|
||||
const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'assets-manifest.json'), 'utf8'));
|
||||
for (const name of [...(manifest.js || []), ...(manifest.css || [])]) {
|
||||
if (!fs.statSync(path.join(dir, name)).isFile()) throw new Error(`Missing asset: ${subdir}/${name}`);
|
||||
}
|
||||
}
|
||||
const native = require(path.join(app, 'dist/server-native.node'));
|
||||
if (Object.keys(native).length < 1) throw new Error('Empty native module');
|
||||
console.log('Source-built native module and all three frontend manifests verified');
|
||||
JS
|
||||
|
||||
node "$AFFINE_CLIENT_TEST"
|
||||
|
||||
# Check idempotent predeploy against an already migrated, running installation.
|
||||
if ! "$AFFINE_PACKAGE/bin/affine-migrate" > "$TMPDIR/migrate-again.log" 2>&1; then
|
||||
cat "$TMPDIR/migrate-again.log"
|
||||
exit 1
|
||||
fi
|
||||
cmp "$TMPDIR/original-private.key" "$HOME/.affine/config/private.key"
|
||||
@@ -11,6 +11,7 @@ in
|
||||
(import ./db-tool { inherit system inputs self pkgs; }) //
|
||||
(import ./with-attic-cache { inherit system inputs self pkgs; }) //
|
||||
(import ./element-web { inherit system inputs self pkgs; }) //
|
||||
(import ./affine { inherit system inputs self pkgs; }) //
|
||||
(import ./linux-devshell { inherit system inputs self pkgs; }) //
|
||||
(import ./windows-devshell { inherit system inputs self pkgs; }) //
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user