Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieusieben committed Feb 13, 2024
1 parent fc7a9d9 commit a631b90
Show file tree
Hide file tree
Showing 61 changed files with 417 additions and 228 deletions.
18 changes: 9 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@
"plugin:prettier/recommended",
"prettier"
],
"ignorePatterns": [
"dist",
"node_modules",
"jest.config.base.js",
"jest.bench.config.js",
"jest.config.js",
"build.js",
"update-pkg.js"
],
"ignorePatterns": ["dist", "node_modules"],
"rules": {
"no-var": "error",
"prefer-const": "warn",
Expand All @@ -37,6 +29,14 @@
"@typescript-eslint/no-explicit-any": "off"
},
"overrides": [
{
"files": ["jest.config.js"],
"env": { "commonjs": true }
},
{
"files": ["jest.setup.js"],
"env": { "jest": true }
},
{
"files": "*.js",
"rules": {
Expand Down
20 changes: 0 additions & 20 deletions jest.config.base.js

This file was deleted.

5 changes: 1 addition & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// jest.config.js
const base = require('./jest.config.base.js')

/** @type {import('jest').Config} */
module.exports = {
...base,
projects: ['<rootDir>/packages/*/jest.config.js'],
}
File renamed without changes.
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
},
"scripts": {
"lint:fix": "pnpm lint --fix",
"lint": "eslint . --ext .ts,.tsx",
"verify": "prettier --check . && pnpm lint",
"format": "prettier --write .",
"lint": "eslint . --ext .ts,.js",
"style:fix": "prettier --write .",
"style": "prettier --check .",
"verify": "npm-run-all -p verify:*",
"verify:style": "pnpm run style",
"verify:lint": "pnpm lint",
"verify:types": "tsc --build tsconfig.json",
"format": "npm-run-all *:fix",
"build": "pnpm -r --stream build",
"dev": "npm-run-all -p dev:*",
"dev:tsc": "tsc --build tsconfig.json --watch",
Expand Down Expand Up @@ -40,7 +45,6 @@
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"handlebars-jest": "^1.0.0",
"jest": "^28.1.2",
"node-gyp": "^9.3.1",
"npm-run-all": "^4.1.5",
Expand Down
9 changes: 6 additions & 3 deletions packages/api/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const base = require('../../jest.config.base.js')

/** @type {import('jest').Config} */
module.exports = {
...base,
displayName: 'API',
transform: { '^.+\\.(t|j)s$': '@swc/jest' },
transformIgnorePatterns: [`<rootDir>/node_modules/(?!get-port)`],
testTimeout: 60000,
setupFiles: ['<rootDir>/../../jest.setup.ts'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
}
20 changes: 20 additions & 0 deletions packages/api/jest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
declare namespace jest {
// eslint-disable-next-line
interface Matchers<R, T = {}> {
toBeModerationResult(
expected: ModerationBehaviorResult | undefined,
context: string,
stringifiedResult: string,
ignoreCause?: boolean,
): R
}

interface Expect {
toBeModerationResult(
expected: ModerationBehaviorResult | undefined,
context: string,
stringifiedResult: string,
ignoreCause?: boolean,
): void
}
}
79 changes: 79 additions & 0 deletions packages/api/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ModerationBehaviorResult } from './definitions/moderation-behaviors'
import { ModerationUI } from './src'

expect.extend({
toBeModerationResult(
actual: ModerationUI,
expected: ModerationBehaviorResult | undefined,
context: string,
stringifiedResult: string,
ignoreCause = false,
) {
const fail = (msg: string) => ({
pass: false,
message: () => `${msg}. Full result: ${stringifiedResult}`,
})
let cause = actual.cause?.type as string
if (actual.cause?.type === 'label') {
cause = `label:${actual.cause.labelDef.id}`
} else if (actual.cause?.type === 'muted') {
if (actual.cause.source.type === 'list') {
cause = 'muted-by-list'
}
} else if (actual.cause?.type === 'blocking') {
if (actual.cause.source.type === 'list') {
cause = 'blocking-by-list'
}
}
if (!expected) {
if (!ignoreCause && actual.cause) {
return fail(`${context} expected to be a no-op, got ${cause}`)
}
if (actual.alert) {
return fail(`${context} expected to be a no-op, got alert=true`)
}
if (actual.blur) {
return fail(`${context} expected to be a no-op, got blur=true`)
}
if (actual.filter) {
return fail(`${context} expected to be a no-op, got filter=true`)
}
if (actual.noOverride) {
return fail(`${context} expected to be a no-op, got noOverride=true`)
}
} else {
if (!ignoreCause && cause !== expected.cause) {
return fail(`${context} expected to be ${expected.cause}, got ${cause}`)
}
if (!!actual.alert !== !!expected.alert) {
return fail(
`${context} expected to be alert=${expected.alert || false}, got ${
actual.alert || false
}`,
)
}
if (!!actual.blur !== !!expected.blur) {
return fail(
`${context} expected to be blur=${expected.blur || false}, got ${
actual.blur || false
}`,
)
}
if (!!actual.filter !== !!expected.filter) {
return fail(
`${context} expected to be filter=${expected.filter || false}, got ${
actual.filter || false
}`,
)
}
if (!!actual.noOverride !== !!expected.noOverride) {
return fail(
`${context} expected to be noOverride=${
expected.noOverride || false
}, got ${actual.noOverride || false}`,
)
}
}
return { pass: true, message: () => '' }
},
})
3 changes: 2 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"devDependencies": {
"@atproto/lex-cli": "workspace:^",
"common-tags": "^1.8.2"
"common-tags": "^1.8.2",
"jest": "^28.1.2"
}
}
1 change: 0 additions & 1 deletion packages/api/tests/moderation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { moderateProfile, moderatePost } from '../src'
import { mock } from './util'
import './util/moderation-behavior'

describe('Moderation', () => {
it('Applies self-labels on profiles according to the global preferences', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/api/tests/profile-moderation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ describe('Post moderation behaviors', () => {
suiteRunner.moderationOpts(scenario),
)
expect(res.account).toBeModerationResult(
// @ts-expect-error FIXME remove this comment (and fix the TS error)
scenario.behaviors.account,
'account',
JSON.stringify(res, null, 2),
)
expect(res.profile).toBeModerationResult(
// @ts-expect-error FIXME remove this comment (and fix the TS error)
scenario.behaviors.profile,
'profile content',
JSON.stringify(res, null, 2),
Expand Down
82 changes: 2 additions & 80 deletions packages/api/tests/util/moderation-behavior.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,10 @@
import { ModerationUI, ModerationOpts, ComAtprotoLabelDefs } from '../../src'
import type {
ModerationBehaviors,
ModerationBehaviorScenario,
ModerationBehaviorResult,
ModerationBehaviors,
} from '../../definitions/moderation-behaviors'
import { ComAtprotoLabelDefs, ModerationOpts } from '../../src'
import { mock as m } from './index'

expect.extend({
toBeModerationResult(
actual: ModerationUI,
expected: ModerationBehaviorResult | undefined,
context: string,
stringifiedResult: string,
ignoreCause = false,
) {
const fail = (msg: string) => ({
pass: false,
message: () => `${msg}. Full result: ${stringifiedResult}`,
})
let cause = actual.cause?.type as string
if (actual.cause?.type === 'label') {
cause = `label:${actual.cause.labelDef.id}`
} else if (actual.cause?.type === 'muted') {
if (actual.cause.source.type === 'list') {
cause = 'muted-by-list'
}
} else if (actual.cause?.type === 'blocking') {
if (actual.cause.source.type === 'list') {
cause = 'blocking-by-list'
}
}
if (!expected) {
if (!ignoreCause && actual.cause) {
return fail(`${context} expected to be a no-op, got ${cause}`)
}
if (actual.alert) {
return fail(`${context} expected to be a no-op, got alert=true`)
}
if (actual.blur) {
return fail(`${context} expected to be a no-op, got blur=true`)
}
if (actual.filter) {
return fail(`${context} expected to be a no-op, got filter=true`)
}
if (actual.noOverride) {
return fail(`${context} expected to be a no-op, got noOverride=true`)
}
} else {
if (!ignoreCause && cause !== expected.cause) {
return fail(`${context} expected to be ${expected.cause}, got ${cause}`)
}
if (!!actual.alert !== !!expected.alert) {
return fail(
`${context} expected to be alert=${expected.alert || false}, got ${
actual.alert || false
}`,
)
}
if (!!actual.blur !== !!expected.blur) {
return fail(
`${context} expected to be blur=${expected.blur || false}, got ${
actual.blur || false
}`,
)
}
if (!!actual.filter !== !!expected.filter) {
return fail(
`${context} expected to be filter=${expected.filter || false}, got ${
actual.filter || false
}`,
)
}
if (!!actual.noOverride !== !!expected.noOverride) {
return fail(
`${context} expected to be noOverride=${
expected.noOverride || false
}, got ${actual.noOverride || false}`,
)
}
}
return { pass: true, message: () => '' }
},
})

export class ModerationBehaviorSuiteRunner {
constructor(public suite: ModerationBehaviors) {}

Expand Down
1 change: 1 addition & 0 deletions packages/api/tsconfig.tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../../tsconfig/test.json",
"compilerOptions": {
"rootDir": "./",
"types": ["jest", "./jest.d.ts"],
"noEmit": true
},
"include": ["./tests"]
Expand Down
5 changes: 1 addition & 4 deletions packages/aws/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"include": [],
"references": [
{ "path": "./tsconfig.build.json" },
{ "path": "./tsconfig.tests.json" }
]
"references": [{ "path": "./tsconfig.build.json" }]
}
8 changes: 0 additions & 8 deletions packages/aws/tsconfig.tests.json

This file was deleted.

8 changes: 5 additions & 3 deletions packages/bsky/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const base = require('../../jest.config.base.js')

/** @type {import('jest').Config} */
module.exports = {
...base,
displayName: 'Bsky App View',
transform: { '^.+\\.(t|j)s$': '@swc/jest' },
transformIgnorePatterns: [`<rootDir>/node_modules/(?!get-port)`],
testTimeout: 60000,
setupFiles: ['<rootDir>/../../jest.setup.ts'],
}
1 change: 1 addition & 0 deletions packages/bsky/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@types/pg": "^8.6.6",
"@types/qs": "^6.9.7",
"axios": "^0.27.2",
"jest": "^28.1.2",
"ts-node": "^10.8.2"
}
}
1 change: 1 addition & 0 deletions packages/bsky/tests/auto-moderator/labeler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('labeler', () => {
dbPostgresSchema: 'bsky_labeler',
})
ozone = network.ozone
// @ts-expect-error Error due to circular dependency with the dev-env package
ctx = network.bsky.indexer.ctx
const pdsCtx = network.pds.ctx
autoMod = ctx.autoMod
Expand Down
1 change: 1 addition & 0 deletions packages/bsky/tests/duplicate-records.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('duplicate record', () => {
dbPostgresSchema: 'bsky_duplicates',
})
db = network.bsky.indexer.ctx.db
// @ts-expect-error Error due to circular dependency with the dev-env package
services = network.bsky.indexer.ctx.services
did = 'did:example:alice'
})
Expand Down
Loading

0 comments on commit a631b90

Please sign in to comment.