Skip to content

Commit

Permalink
Merge branch 'main' into just-docker-run-uses-docker-compose-ctrl-c-w…
Browse files Browse the repository at this point in the history
…orks-now--fdietze-2024-05-28
  • Loading branch information
fdietze authored May 28, 2024
2 parents 8a45c7a + d7d73e4 commit 28a2339
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 37 deletions.
2 changes: 0 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
LITEFS_DIR=/litefs/data

SOCIAL_PROTOCOLS_DATADIR=$HOME/social-protocols-data
VOTE_EVENTS_PATH=$SOCIAL_PROTOCOLS_DATADIR/vote-events.jsonl
SCORE_EVENTS_PATH=$SOCIAL_PROTOCOLS_DATADIR/score-events.jsonl
APP_DATABASE_PATH=$SOCIAL_PROTOCOLS_DATADIR/sqlite.db
APP_DATABASE_URL=file:$APP_DATABASE_PATH?connection_limit=1
GB_DATABASE_PATH="$SOCIAL_PROTOCOLS_DATADIR/global-brain.db"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
uses: actions/checkout@v4

- name: Test
run: earthly +ci-test
run: earthly --allow-privileged --no-output +ci-test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ node_modules/
.env
.DS_Store
GlobalBrain.jl/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
46 changes: 36 additions & 10 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ app-setup:
app-build:
FROM +app-setup
RUN npm run build
SAVE ARTIFACT server-build AS LOCAL server-build
SAVE ARTIFACT build AS LOCAL build
SAVE ARTIFACT public AS LOCAL public
SAVE ARTIFACT node_modules AS LOCAL node_modules
SAVE ARTIFACT package-lock.json AS LOCAL package-lock.json
SAVE ARTIFACT package.json AS LOCAL package.json
SAVE ARTIFACT server-build
SAVE ARTIFACT build
SAVE ARTIFACT public
SAVE ARTIFACT node_modules
SAVE ARTIFACT package-lock.json
SAVE ARTIFACT package.json
SAVE ARTIFACT .npmrc

app-deploy-litefs:
FROM flyio/litefs:0.5.10
Expand All @@ -93,7 +94,7 @@ docker-image:

# npm run build
COPY --dir other app server public types index.js tsconfig.json remix.config.js tailwind.config.ts postcss.config.js components.json ./
COPY --dir +app-build/server-build +app-build/build +app-build/public +app-build/node_modules +app-build/package-lock.json +app-build/package.json ./
COPY --dir +app-build/server-build +app-build/build +app-build/public +app-build/node_modules +app-build/package-lock.json +app-build/package.json +app-build/.npmrc ./


# startup & migrations
Expand All @@ -107,15 +108,40 @@ docker-image:
ENV CACHE_DATABASE_PATH="/$LITEFS_DIR/$CACHE_DATABASE_FILENAME"
ENV INTERNAL_PORT="8080"
ENV PORT="8081"
ENV VOTE_EVENTS_PATH=/data/vote-events.jsonl
ENV SCORE_EVENTS_PATH=/data/score-events.jsonl

RUN nix-collect-garbage
RUN du -sh /* \
&& find /app -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 du -sh | sort -hr | head -20 \
&& find /nix/store -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 du -sh | sort -hr | head -20 \
&& find /app/node_modules -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 du -sh | sort -hr | head -20

# starting the application is defined in litefs.yml
# test locally without litefs:
# docker run -e SESSION_SECRET -e INTERNAL_COMMAND_TOKEN -e HONEYPOT_SECRET sha256:xyzxyz /bin/sh startup.sh
CMD ["/bin/sh", "-c", "/usr/local/bin/litefs mount"]
SAVE IMAGE jabble:latest

docker-image-e2e-test:
# set up an image with dind (docker in docker) and nix
FROM earthly/dind:alpine-3.19-docker-25.0.5-r0
RUN apk add curl \
&& curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux \
--extra-conf "sandbox = false" \
--init none \
--no-confirm
ENV PATH="${PATH}:/nix/var/nix/profiles/default/bin"
WORKDIR /app
COPY flake.nix flake.lock .
RUN nix develop ".#e2e" --command echo warmed up
COPY --dir e2e playwright.config.ts ./
COPY docker-compose.yml ./
WITH DOCKER --load jabble:latest=+docker-image
RUN docker image ls \
&& (docker-compose up &) \
&& echo waiting for http server to come online... \
&& timeout 60s sh -c 'until curl --silent --fail http://localhost:8081 > /dev/null; do sleep 1; done' \
&& CI=true nix develop --impure ".#e2e" --command playwright test
END

app-deploy:
# run locally:
Expand Down Expand Up @@ -146,7 +172,7 @@ app-lint:
ci-test:
BUILD +app-typecheck
BUILD +app-lint
BUILD +docker-image
BUILD +docker-image-e2e-test

ci-deploy:
# To run manually:
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
jabble:
image: jabble:latest
container_name: jabble
ports:
- "8081:8081"
environment:
- SESSION_SECRET=super-duper-s3cret
- HONEYPOT_SECRET=super-duper-s3cret
- INTERNAL_COMMAND_TOKEN=some-made-up-token
command: /bin/sh startup.sh
init: true # make ctrl+c work
restart: no
7 changes: 7 additions & 0 deletions e2e/example.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test, expect } from '@playwright/test'

test('has title', async ({ page }) => {
await page.goto('http://localhost:8081')

await expect(page).toHaveTitle(/Jabble/)
})
18 changes: 18 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@
python3 # for node-gyp
gcc # for node-gyp

playwright-driver.browsers # e2e tests
playwright-test # e2e tests

earthly
docker
docker-compose
flyctl

# darwin.apple_sdk.frameworks.Security
];
shellHook = ''
export PLAYWRIGHT_BROWSERS_PATH="${pkgs.playwright-driver.browsers}"
export PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS=true
'';
};
build = pkgs.mkShellNoCC {
buildInputs = with pkgs; [
Expand All @@ -44,6 +52,16 @@
gcc # for node-gyp
];
};
e2e = pkgs.mkShellNoCC {
buildInputs = with pkgs; [
playwright-driver.browsers # e2e tests
playwright-test # e2e tests
];
shellHook = ''
export PLAYWRIGHT_BROWSERS_PATH="${pkgs.playwright-driver.browsers}"
export PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS=true
'';
};
production = pkgs.mkShellNoCC {
buildInputs = with pkgs; [
nodejs_20
Expand Down
3 changes: 0 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ reset-db:
reset-all:
rm -rf ~/social-protocols-data/*
mkdir -p ~/social-protocols-data
touch $SCORE_EVENTS_PATH
rm -f "$GB_DATABASE_PATH"
just reset-db

Expand Down Expand Up @@ -89,8 +88,6 @@ download-production-data:
# todo: use sqlite .backup command and download copy
rm -rf $SOCIAL_PROTOCOLS_DATADIR/production/
mkdir -p $SOCIAL_PROTOCOLS_DATADIR/production/
fly ssh sftp get /data/score-events.jsonl $SOCIAL_PROTOCOLS_DATADIR/production/score-events.jsonl
fly ssh sftp get /data/vote-events.jsonl $SOCIAL_PROTOCOLS_DATADIR/production/vote-events.jsonl
fly ssh sftp get /litefs/data/sqlite.db $SOCIAL_PROTOCOLS_DATADIR/production/sqlite.db
fly ssh sftp get /litefs/data/global-brain.db $SOCIAL_PROTOCOLS_DATADIR/production/global-brain.db
fly ssh sftp get /litefs/data/sqlite.db $SOCIAL_PROTOCOLS_DATADIR/production/sqlite.db-wal
Expand Down
78 changes: 57 additions & 21 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,77 @@
import { defineConfig, devices } from '@playwright/test'
import 'dotenv/config'

const PORT = process.env.PORT || '3000'
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests/e2e',
timeout: 15 * 1000,
expect: {
timeout: 5 * 1000,
},
testDir: './e2e',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
baseURL: `http://localhost:${PORT}/`,
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
use: { ...devices['Desktop Chrome'] },
},

// {
// name: 'firefox',
// use: { ...devices['Desktop Firefox'] },
// },
//
// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

webServer: {
command: process.env.CI ? 'npm run start:mocks' : 'npm run dev',
port: Number(PORT),
reuseExistingServer: !process.env.CI,
stdout: 'pipe',
stderr: 'pipe',
env: {
PORT,
},
},
/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
})
4 changes: 4 additions & 0 deletions startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail
set -Eeuo pipefail

echo create a backup before migrating...
sqlite3 /litefs/data/sqlite.db '.backup /data/backup.db'
ls -lh /data/backup.db

npm run migrate

npm start

0 comments on commit 28a2339

Please sign in to comment.