forked from janeirodigital/sai-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: elf Pavlik <[email protected]>
- Loading branch information
1 parent
9d911d7
commit 7b50f2e
Showing
10 changed files
with
519 additions
and
11 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...s-storage-fixture/test/.internal/accounts/data/46b9339e-3cd6-4594-b40b-6bcc28cb881a$.json
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/css-storage-fixture/test/.internal/setup/current-server-version$.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"key":"setup/current-server-version","payload":"7.1.1"} | ||
{"key":"setup/current-server-version","payload":"7.1.2"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
1. select account from accounts | ||
2. create session | ||
3. select operation for example: | ||
- data registries | ||
- select registry from list of registries | ||
- list data registration from registry | ||
- add data registration to registry | ||
- select shape tree from list of shape trees | ||
- go back | ||
*/ | ||
|
||
import * as Command from '@effect/cli/Command'; | ||
import * as Prompt from '@effect/cli/Prompt'; | ||
import * as NodeContext from '@effect/platform-node/NodeContext'; | ||
import * as Runtime from '@effect/platform-node/NodeRuntime'; | ||
import { Context, Effect, Layer } from 'effect'; | ||
|
||
import { type Account, accounts, shapeTree, createApp, SolidTestUtils } from '@janeirodigital/css-test-utils'; | ||
import { AuthorizationAgent } from '@janeirodigital/interop-authorization-agent'; | ||
import { asyncIterableToArray } from '@janeirodigital/interop-utils'; | ||
import { init } from '@paralleldrive/cuid2'; | ||
|
||
const cuid = init({ length: 6 }); | ||
|
||
console.log('Server starting'); | ||
const server = await createApp(); | ||
await server.start(); | ||
console.log('Server started'); | ||
|
||
class SessionManager extends Context.Tag('SessionManager')< | ||
SessionManager, | ||
{ readonly getSession: (account: Account) => Effect.Effect<AuthorizationAgent> } | ||
>() {} | ||
|
||
const SessionManagerLive = Layer.succeed( | ||
SessionManager, | ||
SessionManager.of({ | ||
getSession: (account: Account) => Effect.promise(() => buildSession(account)) | ||
}) | ||
); | ||
|
||
const createSelectDataRegistryPrompt = (registryList: string[]) => { | ||
return Prompt.select({ | ||
message: 'Pick data registry', | ||
choices: registryList.map((registry) => ({ title: registry, value: registry })) | ||
}); | ||
}; | ||
|
||
const createSelectShapeTreePrompt = (shapeTreeList: string[]) => { | ||
return Prompt.select({ | ||
message: 'Pick shape tree', | ||
choices: shapeTreeList.map((st) => ({ title: st, value: st })) | ||
}); | ||
}; | ||
|
||
const accountPrompt = Prompt.select({ | ||
message: 'Pick account', | ||
choices: Object.keys(accounts).map((accountName: keyof typeof accounts) => ({ | ||
title: accountName, | ||
value: accountName | ||
})) | ||
}); | ||
|
||
enum Actions { | ||
createDataRegistration = 'createDataRegistration', | ||
createSocialAgentRegistration = 'createSocialAgentRegistration' | ||
} | ||
|
||
const selectActionPrompt = Prompt.select({ | ||
message: 'Select action', | ||
choices: [ | ||
{ title: 'Create data registration', value: Actions.createDataRegistration }, | ||
{ title: 'Create social agent registration', value: Actions.createSocialAgentRegistration } | ||
] | ||
}); | ||
|
||
const getSession = Effect.gen(function* () { | ||
const sessionManager = yield* SessionManager; | ||
const account = yield* accountPrompt; | ||
return yield* sessionManager.getSession(accounts[account]); | ||
}); | ||
|
||
const mainPrompt = Effect.provide( | ||
Effect.gen(function* () { | ||
const sessionManager = yield* SessionManager; | ||
|
||
const action = yield* selectActionPrompt; | ||
|
||
switch (action) { | ||
case Actions.createDataRegistration: | ||
yield* createDataRegistration.pipe(Effect.provideService(SessionManager, sessionManager)); | ||
break; | ||
case Actions.createSocialAgentRegistration: | ||
yield* createSocialAgentRegistration.pipe(Effect.provideService(SessionManager, sessionManager)); | ||
break; | ||
} | ||
}), | ||
SessionManagerLive | ||
); | ||
|
||
const createDataRegistration = Effect.gen(function* () { | ||
const sessionManager = yield* SessionManager; | ||
|
||
const session = yield* getSession.pipe(Effect.provideService(SessionManager, sessionManager)); | ||
|
||
const registryId = yield* createSelectDataRegistryPrompt(session.registrySet.hasDataRegistry.map(({ iri }) => iri)); | ||
|
||
const registry = session.registrySet.hasDataRegistry.find(({ iri }) => iri === registryId)!; | ||
|
||
const registrations = yield* Effect.promise(async () => asyncIterableToArray(registry.registrations)); | ||
|
||
const existingShapeTrees = registrations.map((registration) => registration.shapeTree.iri); | ||
|
||
existingShapeTrees.forEach((iri) => console.log(iri)); | ||
|
||
const remainingShapeTrees = [...new Set(Object.values(shapeTree)).difference(new Set(existingShapeTrees))]; | ||
|
||
const shapeTreeId = yield* createSelectShapeTreePrompt(remainingShapeTrees); | ||
|
||
yield* Effect.promise(async () => registry.createRegistration(shapeTreeId)); | ||
}); | ||
|
||
const createSocialAgentRegistration = Effect.gen(function* () { | ||
const sessionManager = yield* SessionManager; | ||
|
||
const account = yield* accountPrompt; | ||
|
||
const session = yield* sessionManager.getSession(accounts[account]); | ||
|
||
const webId = yield* Prompt.text({ message: 'Enter social agent name' }); | ||
const label = yield* Prompt.text({ message: 'Enter social agent label' }); | ||
const note = yield* Prompt.text({ message: 'Enter social agent note (optional)' }); | ||
|
||
yield* Effect.promise(async () => | ||
session.registrySet.hasAgentRegistry.addSocialAgentRegistration(webId, label, note) | ||
); | ||
}); | ||
|
||
const command = Command.make('sai', {}, () => mainPrompt); | ||
|
||
const cli = Command.run(command, { | ||
name: 'Prompt Examples', | ||
version: '0.0.1' | ||
}); | ||
|
||
Effect.suspend(() => cli(process.argv)).pipe(Effect.provide(NodeContext.layer), Runtime.runMain); | ||
|
||
async function buildSession(account: Account): Promise<AuthorizationAgent> { | ||
const stu = new SolidTestUtils(account); | ||
await stu.auth(); | ||
return await AuthorizationAgent.build( | ||
account.webId, | ||
`https://auth.example/${account.shortName}`, | ||
{ | ||
fetch: stu.authFetch, | ||
randomUUID: cuid | ||
}, | ||
account.registrySet | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.