Skip to content

Commit

Permalink
test grouping to fix async tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erhant committed Feb 23, 2024
1 parent b9bb472 commit 46b174e
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 147 deletions.
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const config: JestConfigWithTsJest = {
openHandlesTimeout: 10_000,
// print everything like Mocha
verbose: true,
// dont run in parallel
maxConcurrency: 1,
// do this instead of forceExit
detectOpenHandles: true,
// ignore output directory
testPathIgnorePatterns: ["bin", "node_modules", "src"],
};
Expand Down
74 changes: 38 additions & 36 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { resolve } from "path";

type ENTITIES = "REDIS" | "HOLLOWDB" | "HNSW";

const DRIA_ROOT = homedir() + "/.dria"; // "~/.dria/",
/** The main Dria directory is located at $HOME/.dria */
const DRIA_ROOT = homedir() + "/.dria";

/** This will be `true` when we run `yarn test`. */
const IS_TEST = process.env.NODE_ENV === "test";

export default {
ARWEAVE: {
Expand All @@ -24,7 +28,7 @@ export default {
HOLLOWDB: {
/** Timeout until download starts during `pull`,
* if download doesn't start by then, it gives an error. */
DOWNLOAD_TIMEOUT: 15000,
DOWNLOAD_TIMEOUT: IS_TEST ? 100_000 : 15_000,
},
LOGGER: {
NAME: "dria-logger",
Expand All @@ -40,38 +44,36 @@ export default {
HOLLOWDB: "firstbatch/dria-hollowdb:latest",
HNSW: "firstbatch/dria-hnsw:latest",
} as const satisfies Record<ENTITIES, string>,
CONTAINERS:
process.env.NODE_ENV === "test"
? ({
REDIS: "dria-redis-testing",
HOLLOWDB: "dria-hollowdb-testing",
HNSW: "dria-hnsw-testing",
} as const satisfies Record<ENTITIES, `${string}-testing`>)
: ({
REDIS: "dria-redis",
HOLLOWDB: "dria-hollowdb",
HNSW: "dria-hnsw",
} as const satisfies Record<ENTITIES, string>),
NETWORK:
process.env.NODE_ENV === "test"
? {
NAME: "dria-network",
SUBNET: "172.30.0.0/24",
GATEWAY: "172.30.0.1",
IPS: {
REDIS: "172.30.0.11",
HOLLOWDB: "172.30.0.12",
HNSW: "172.30.0.13",
} as const satisfies Record<ENTITIES, `${number}.${number}.${number}.${number}`>,
}
: {
NAME: "dria-network-testing",
SUBNET: "173.30.0.0/24",
GATEWAY: "173.30.0.1",
IPS: {
REDIS: "173.30.0.11",
HOLLOWDB: "173.30.0.12",
HNSW: "173.30.0.13",
} as const satisfies Record<ENTITIES, `${number}.${number}.${number}.${number}`>,
},
CONTAINERS: IS_TEST
? ({
REDIS: "dria-redis-testing",
HOLLOWDB: "dria-hollowdb-testing",
HNSW: "dria-hnsw-testing",
} as const satisfies Record<ENTITIES, `${string}-testing`>)
: ({
REDIS: "dria-redis",
HOLLOWDB: "dria-hollowdb",
HNSW: "dria-hnsw",
} as const satisfies Record<ENTITIES, string>),
NETWORK: IS_TEST
? {
NAME: "dria-network",
SUBNET: "172.30.0.0/24",
GATEWAY: "172.30.0.1",
IPS: {
REDIS: "172.30.0.11",
HOLLOWDB: "172.30.0.12",
HNSW: "172.30.0.13",
} as const satisfies Record<ENTITIES, `${number}.${number}.${number}.${number}`>,
}
: {
NAME: "dria-network-testing",
SUBNET: "173.30.0.0/24",
GATEWAY: "173.30.0.1",
IPS: {
REDIS: "173.30.0.11",
HOLLOWDB: "173.30.0.12",
HNSW: "173.30.0.13",
} as const satisfies Record<ENTITIES, `${number}.${number}.${number}.${number}`>,
},
} as const;
100 changes: 100 additions & 0 deletions tests/commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import cmdFetch from "../src/commands/fetch";
import cmdClear from "../src/commands/clear";
import constants from "../src/constants";
import { existsSync } from "node:fs";
import cmdServe from "../src/commands/serve";
import { driaClient } from "./common";
import cmdStop from "../src/commands/stop";
import { checkDocker, checkNetwork, sleep } from "../src/common";
import cmdPull from "../src/commands/pull";

describe("commands", () => {
beforeAll(async () => {
await checkDocker();
await checkNetwork();
});

describe("fetch", () => {
// a small knowledge zip on Arweave
const txid = "3yUzQ8vnLeFUz_T2mhMoAQFZhJtQYG5o6FfeRRbLm-E";
// the corresponding contract ID from the zip at that txID
const contractId = "WbcY2a-KfDpk7fsgumUtLC2bu4NQcVzNlXWi13fPMlU";
// metadata type of this knowledge
type MetadataType = { id: string; page: string; text: string };

beforeAll(async () => {
await cmdClear(contractId);
});

afterAll(async () => {
await cmdClear(contractId);
});

it("should fetch", async () => {
expect(existsSync(`${constants.DRIA.DATA}/${contractId}`)).toBe(false);
await cmdFetch(txid);
await sleep(2000);
expect(existsSync(`${constants.DRIA.DATA}/${contractId}`)).toBe(true);
});

it("should serve", async () => {
expect(await driaClient.health()).toBe(false);
await cmdServe(contractId);
await sleep(2000);
expect(await driaClient.health()).toBe(true);

const fetched = await driaClient.fetchIds<MetadataType>([0]);
expect(fetched.length).toBe(1);
expect(typeof fetched[0].id).toBe("string");
expect(typeof fetched[0].page).toBe("string");
expect(typeof fetched[0].text).toBe("string");
});

it("should stop", async () => {
await cmdStop();
await sleep(2000);
expect(await driaClient.health()).toBe(false);
});
});

describe("pull", () => {
// a small knowledge about TypeScript (272 entries)
const contractId = "-B64DjhUtCwBdXSpsRytlRQCu-bie-vSTvTIT8Ap3g0";
// metadata type of this knowledge
type MetadataType = { id: string; page: string; text: string };

beforeAll(async () => {
await cmdClear(contractId);
});

afterAll(async () => {
await cmdClear(contractId);
});

it("should pull", async () => {
expect(existsSync(`${constants.DRIA.DATA}/${contractId}`)).toBe(false);
await cmdPull(contractId);
await sleep(2000);
expect(existsSync(`${constants.DRIA.DATA}/${contractId}`)).toBe(true);
});

it("should serve", async () => {
expect(await driaClient.health()).toBe(false);
await cmdServe(contractId);
await sleep(2000);
expect(await driaClient.health()).toBe(true);

const fetched = await driaClient.fetchIds<MetadataType>([0]);
expect(fetched.length).toBe(1);
expect(typeof fetched[0].id).toBe("string");
expect(typeof fetched[0].page).toBe("string");
expect(typeof fetched[0].text).toBe("string");
});

it("should stop", async () => {
await cmdStop();
await sleep(2000);
expect(await driaClient.health()).toBe(false);
});
});
});
56 changes: 0 additions & 56 deletions tests/fetch.test.ts

This file was deleted.

53 changes: 0 additions & 53 deletions tests/pull.test.ts

This file was deleted.

0 comments on commit 46b174e

Please sign in to comment.