Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jan 5, 2024
1 parent 60c7058 commit 113e8f4
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 1,248 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,7 @@ jobs:
- name: Run unit tests
if: ${{ matrix.spec.test }}
shell: bash
run: yarn vitest
working-directory: packages/frontend/electron
run: yarn workspace @affine/electron vitest

- name: Download core artifact
uses: ./.github/actions/download-core
Expand Down
5 changes: 3 additions & 2 deletions packages/backend/server/src/modules/auth/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class AuthService {
exp: now + this.config.auth.accessTokenExpiresIn,
iss: this.config.serverId,
sub: user.id,
aud: user.name,
aud: 'https://affine.pro',
jti: randomUUID({
disableEntropyCache: true,
}),
Expand Down Expand Up @@ -80,7 +80,7 @@ export class AuthService {
iat: now,
iss: this.config.serverId,
sub: user.id,
aud: user.name,
aud: 'https://affine.pro',
jti: randomUUID({
disableEntropyCache: true,
}),
Expand All @@ -100,6 +100,7 @@ export class AuthService {
iss: [this.config.serverId],
leeway: this.config.auth.leeway,
requiredSpecClaims: ['exp', 'iat', 'iss', 'sub'],
aud: ['https://affine.pro'],
})
).data as UserClaim;

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/server/src/modules/auth/utils/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const jwtEncode = async (
exp: now + (maxAge ?? config.auth.accessTokenExpiresIn),
iss: config.serverId,
sub: user.id,
aud: user.name,
aud: 'https://affine.pro',
jti: randomUUID({
disableEntropyCache: true,
}),
Expand Down
6 changes: 5 additions & 1 deletion packages/frontend/electron/test/db/ensure-db.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { setTimeout } from 'node:timers/promises';

import { removeWithRetry } from '@affine-test/kit/utils/utils';
import { v4 } from 'uuid';
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
import { afterAll, afterEach, beforeEach, expect, test, vi } from 'vitest';

const tmpDir = path.join(__dirname, 'tmp');
const appDataPath = path.join(tmpDir, 'app-data');
Expand Down Expand Up @@ -48,6 +48,10 @@ afterEach(async () => {
vi.useRealTimers();
});

afterAll(() => {
vi.doUnmock('@affine/electron/helper/main-rpc');
});

test('can get a valid WorkspaceSQLiteDB', async () => {
const { ensureSQLiteDB } = await import(
'@affine/electron/helper/db/ensure-db'
Expand Down
37 changes: 27 additions & 10 deletions packages/frontend/electron/test/db/migration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
import path from 'node:path';

import {
copyToTemp,
migrateToSubdocAndReplaceDatabase,
} from '@affine/electron/helper/db/migration';
import { SqliteConnection } from '@affine/native';
import { removeWithRetry } from '@affine-test/kit/utils/utils';
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
afterAll,
afterEach,
beforeAll,
describe,
expect,
it,
vi,
} from 'vitest';
import { applyUpdate, Doc as YDoc } from 'yjs';

const tmpDir = path.join(__dirname, 'tmp');
const testDBFilePath = path.resolve(__dirname, 'old-db.affine');

const appDataPath = path.join(tmpDir, 'app-data');

vi.mock('@affine/electron/helper/main-rpc', () => ({
mainRPC: {
getPath: async () => appDataPath,
},
}));
beforeAll(() => {
vi.doMock('@affine/electron/helper/main-rpc', () => ({
mainRPC: {
getPath: async () => appDataPath,
channel: {
on: () => {},
send: () => {},
},
},
}));
});

afterEach(async () => {
await removeWithRetry(tmpDir);
});

afterAll(() => {
vi.doUnmock('@affine/electron/helper/main-rpc');
});

describe('migrateToSubdocAndReplaceDatabase', () => {
it('should migrate and replace the database', async () => {
const { copyToTemp, migrateToSubdocAndReplaceDatabase } = await import(
'@affine/electron/helper/db/migration'
);
const copiedDbFilePath = await copyToTemp(testDBFilePath);
await migrateToSubdocAndReplaceDatabase(copiedDbFilePath);

Expand Down
18 changes: 12 additions & 6 deletions packages/frontend/electron/test/db/workspace-db-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@ import { dbSubjects } from '@affine/electron/helper/db/subjects';
import { removeWithRetry } from '@affine-test/kit/utils/utils';
import fs from 'fs-extra';
import { v4 } from 'uuid';
import { afterEach, expect, test, vi } from 'vitest';
import { afterAll, afterEach, beforeAll, expect, test, vi } from 'vitest';
import { Doc as YDoc, encodeStateAsUpdate } from 'yjs';

const tmpDir = path.join(__dirname, 'tmp');
const appDataPath = path.join(tmpDir, 'app-data');

vi.doMock('@affine/electron/helper/main-rpc', () => ({
mainRPC: {
getPath: async () => appDataPath,
},
}));
beforeAll(() => {
vi.doMock('@affine/electron/helper/main-rpc', () => ({
mainRPC: {
getPath: async () => appDataPath,
},
}));
});

afterEach(async () => {
await removeWithRetry(tmpDir);
});

afterAll(() => {
vi.doUnmock('@affine/electron/helper/main-rpc');
});

let testYDoc: YDoc;
let testYSubDoc: YDoc;

Expand Down
6 changes: 5 additions & 1 deletion packages/frontend/electron/test/workspace/handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import { removeWithRetry } from '@affine-test/kit/utils/utils';
import fs from 'fs-extra';
import { v4 } from 'uuid';
import { afterEach, describe, expect, test, vi } from 'vitest';
import { afterAll, afterEach, describe, expect, test, vi } from 'vitest';

const tmpDir = path.join(__dirname, 'tmp');
const appDataPath = path.join(tmpDir, 'app-data');
Expand All @@ -24,6 +24,10 @@ afterEach(async () => {
await removeWithRetry(tmpDir);
});

afterAll(() => {
vi.doUnmock('@affine/electron/helper/main-rpc');
});

describe('list workspaces', () => {
test('listWorkspaces (valid)', async () => {
const { listWorkspaces } = await import(
Expand Down
44 changes: 44 additions & 0 deletions packages/frontend/native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,35 @@ switch (platform) {
loadError = e
}
break
case 'riscv64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'affine.linux-riscv64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./affine.linux-riscv64-musl.node')
} else {
nativeBinding = require('@affine/native-linux-riscv64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'affine.linux-riscv64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./affine.linux-riscv64-gnu.node')
} else {
nativeBinding = require('@affine/native-linux-riscv64-gnu')
}
} catch (e) {
loadError = e
}
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${arch}`)
}
Expand All @@ -277,6 +306,21 @@ switch (platform) {
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
}

if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
try {
nativeBinding = require('./affine.wasi.cjs')
} catch {
// ignore
}
if (!nativeBinding) {
try {
nativeBinding = require('@affine/native-wasm32-wasi')
} catch (err) {
console.error(err)
}
}
}

if (!nativeBinding) {
if (loadError) {
throw loadError
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default defineConfig({
'**/dist',
'**/build',
'**/out,',
'**/frontend/electron',
'**/packages/frontend/electron',
],
testTimeout: 5000,
coverage: {
Expand Down
Loading

0 comments on commit 113e8f4

Please sign in to comment.