Skip to content

Commit

Permalink
Merge pull request #16 from desci-labs/m0ar/m0ar-tests
Browse files Browse the repository at this point in the history
m0ar tests
  • Loading branch information
m0ar authored Oct 16, 2023
2 parents cd4cbe2 + 08f6133 commit 3637787
Show file tree
Hide file tree
Showing 15 changed files with 1,230 additions and 743 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ jobs:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: setup-node@v3
- uses: actions/setup-node@v3
with:
node-version-file: ".nvmrc"
cache: "npm"
cache-dependency-path: "package-lock.json"
check-latest: false
- run: npm ci
- run: npm run generate
- run: make test
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ clean-test:
rm -rf local-data/ceramic-test

test: clean-test
sed 's|local-data/ceramic|local-data/ceramic-test|' composedb.config.json \
> test.config.json
sed 's|local-data/ceramic|local-data/ceramic-test|' composedb.config.json > test.config.json
npx ceramic daemon --config test.config.json &>/dev/null &
sleep 5
npm test; pkill --full "npm exec ceramic"
node scripts/composites.mjs
# Kill daemons without losing test exit code for CI
if npm test; then \
npm run kill; true; \
else \
npm run kill; false; \
fi
8 changes: 4 additions & 4 deletions components/ResearchObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import Link from "next/link";
import styles from "@/styles/Home.module.scss";

import { ROProps } from "@/types";
import { ResearchObject } from "@/types";
import { PropsWithChildren } from "react";

const ResearchObject = ({
const ResearchObjectComponent = ({
owner,
title,
manifest,
children,
}: PropsWithChildren<ROProps>) => {
}: PropsWithChildren<ResearchObject>) => {
return (
<div className={styles.post}>
<div><big>{title}</big></div>
Expand All @@ -22,4 +22,4 @@ const ResearchObject = ({
);
};

export default ResearchObject;
export default ResearchObjectComponent;
4 changes: 2 additions & 2 deletions components/ResearchObjectForm.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use client";
import { useCeramicContext } from "@/context";
import { ROProps } from "@/types";
import { ResearchObject } from "@/types";
import styles from "@/styles/profile.module.scss";
import { useState } from "react";
import { mutationCreateResearchObject } from "@/utils/queries";

export const ResearchObjectForm = (updateParent: () => void) => {
const { ceramic, composeClient } = useCeramicContext();
const [object, setObject] = useState<ROProps>({
const [object, setObject] = useState<ResearchObject>({
title: "",
manifest: ""
});
Expand Down
2 changes: 1 addition & 1 deletion composites/11-annotation.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Annotation
# Optionally tag a claim to contextualise the annotation
claimID: StreamID @documentReference(model: "Claim")
claim: Claim @relationDocument(property: "claimID")
claimVersion: CommitID!
claimVersion: CommitID

metadataPayload: String @string(maxLength: 1024)
}
1,170 changes: 580 additions & 590 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"start": "next start",
"lint": "next lint",
"ceramic": "ceramic daemon --config composedb.config.json",
"kill": "pkill --full \"ceramic daemon|ipfs daemon\"",
"test": "vitest --run --config vitest.config.ts"
},
"author": "Edvard Hubinette",
Expand Down
10 changes: 5 additions & 5 deletions pages/explore.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { NextPage } from "next";
import { useCallback, useEffect, useState } from "react";
import { useCeramicContext } from "@/context";
import { ROProps } from "@/types";
import { ResearchObject } from "@/types";
import Head from "next/head";
import styles from "@/styles/Home.module.scss";
import React from "react";
import ResearchObject from "@/components/ResearchObject";
import ResearchObjectComponent from "@/components/ResearchObject";
import { queryResearchObjects } from "@/utils/queries";
import { AttestButton } from "@/components/AttestButton";
import { AttestList } from "@/components/AttestList";

const ExplorePage: NextPage = () => {
const clients = useCeramicContext();
const { composeClient } = clients;
const [objects, setObjects] = useState<ROProps[] | []>([]);
const [objects, setObjects] = useState<ResearchObject[] | []>([]);

const getResearchObjects = useCallback(async () => {
const researchObjects = await queryResearchObjects(composeClient);
Expand All @@ -36,7 +36,7 @@ const ExplorePage: NextPage = () => {
<big>The world of DeSci</big>
</label>
{objects.map((ro) => (
<ResearchObject
<ResearchObjectComponent
key={ro.id}
id={ro.id}
title={ro.title}
Expand All @@ -45,7 +45,7 @@ const ExplorePage: NextPage = () => {
>
<AttestButton targetID={ro.id!} />
<AttestList targetID={ro.id!} />
</ResearchObject>
</ResearchObjectComponent>
))}
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { NextPage } from "next";
import { useCallback, useEffect, useState } from "react";
import { useCeramicContext } from "@/context";
import { ROProps } from "@/types";
import { ResearchObject } from "@/types";
import Head from "next/head";
import styles from "@/styles/Home.module.scss";
import React from "react";
import ResearchObject from "@/components/ResearchObject";
import ResearchObjectComponent from "@/components/ResearchObject";
import { ResearchObjectForm } from "@/components/ResearchObjectForm";
import { queryViewerId, queryViewerResearchObjects } from "@/utils/queries";

const Home: NextPage = () => {
const clients = useCeramicContext();
const { ceramic, composeClient } = clients;
const [objects, setObjects] = useState<ROProps[] | []>([]);
const [objects, setObjects] = useState<ResearchObject[] | []>([]);

const getResearchObjects = useCallback(async () => {
if (ceramic.did !== undefined) {
Expand Down Expand Up @@ -43,7 +43,7 @@ const Home: NextPage = () => {
<big>My research objects</big>
</label>
{objects.map((ro) => (
<ResearchObject
<ResearchObjectComponent
key={ro.id}
id={ro.id}
title={ro.title}
Expand Down
69 changes: 38 additions & 31 deletions scripts/commands.mjs
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import KeyDIDResolver from "key-did-resolver";
import { getResolver } from "key-did-resolver";
import { randomBytes } from "crypto";
import { toString } from "uint8arrays/to-string";
import { writeFile } from "fs";
import { fromString } from "uint8arrays/from-string";
import { existsSync, readFileSync, writeFileSync } from "fs";
import { DID } from "dids";
import { Ed25519Provider } from "key-did-provider-ed25519";

const PWD = process.cwd();
const CONFIG_PATH = `${PWD}/composedb.config.json`;
const SEED_PATH = `${PWD}/admin_seed.txt`;

export const RunCommands = async () => {
const newSeed = () => {
const raw = new Uint8Array(randomBytes(32));
return toString(raw, 'base16')
}

const generateAdminKeyDid = async () => {
const seed = new Uint8Array(randomBytes(32));
const keyResolver = KeyDIDResolver.getResolver();
const seed = readFileSync(SEED_PATH);
const key = fromString(seed, 'base16');
const did = new DID({
provider: new Ed25519Provider(seed),
resolver: {
...keyResolver,
},
provider: new Ed25519Provider(key),
resolver: getResolver(),
});
await did.authenticate();
return {
seed: toString(seed, "base16"),
did,
};
return did;
};
const generateLocalConfig = async (adminSeed, adminDid) => {

const generateLocalConfig = async (adminDid) => {
const configData = {
anchor: {},
"http-api": {
Expand Down Expand Up @@ -49,30 +55,31 @@ export const RunCommands = async () => {
"local-directory": "local-data/ceramic/statestore",
},
indexing: {
db: `sqlite://${process.cwd()}/local-data/ceramic/indexing.sqlite`,
db: `sqlite://${PWD}/local-data/ceramic/indexing.sqlite`,
"allow-queries-before-historical-sync": true,
// Cannot be enabled on inmemory, but activate for proper networks
// "enable-historical-sync": "true"
models: [],
},
};
writeFile(
`${process.cwd()}/composedb.config.json`,
JSON.stringify(configData),
(err) => {
if (err) {
console.error(err);
}
}
);
writeFile(`${process.cwd()}/admin_seed.txt`, adminSeed, (err) => {
if (err) {
console.error(err);
}
});
writeFileSync(CONFIG_PATH, JSON.stringify(configData, undefined, 2));
};

if (!existsSync(SEED_PATH)){
console.log('Creating new admin seed...');
writeFileSync(SEED_PATH, newSeed());

console.log('Generating new config...');
const did = await generateAdminKeyDid();
console.log('Saving new DID:', JSON.stringify(did, undefined, 2))
await generateLocalConfig(did);
} else if (!existsSync(CONFIG_PATH)) {
console.log('Found seed but no config, generating...');
const did = await generateAdminKeyDid();
await generateLocalConfig(did);
} else {
console.log('Seed and config present, skipping generation.')
};
const { seed, did } = await generateAdminKeyDid();
console.log(seed, did);
await generateLocalConfig(seed, did);
};

RunCommands();
14 changes: 11 additions & 3 deletions scripts/composites.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ceramic = new CeramicClient("http://localhost:7007");
* @return {Promise<void>} - return void when composite finishes deploying.
*/
export const writeComposite = async (spinner) => {
await authenticate();
await authenticateAdmin();
spinner.info("writing composite to Ceramic");

const profileComposite = await createComposite(
Expand Down Expand Up @@ -165,14 +165,22 @@ export const writeComposite = async (spinner) => {
* Authenticating DID for publishing composite
* @return {Promise<void>} - return void when DID is authenticated.
*/
const authenticate = async () => {
const authenticateAdmin = async () => {
const seed = readFileSync("./admin_seed.txt");
const key = fromString(seed, "base16");
const did = new DID({
resolver: getResolver(),
provider: new Ed25519Provider(key),
});
await did.authenticate();
ceramic.did = did;
await ceramic.setDID(did);
};

const runAsScript =
process.argv[0].includes('/bin/node') &&
process.argv[1].includes('scripts/composites.mjs');

if (runAsScript) {
const logSpinner = { info: console.log, succeed: console.log };
await writeComposite(logSpinner);
};
Loading

0 comments on commit 3637787

Please sign in to comment.