Skip to content

Commit

Permalink
refactor: adjust export
Browse files Browse the repository at this point in the history
  • Loading branch information
boan-anbo committed Nov 27, 2023
1 parent de68ad4 commit 827a9e4
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 51 deletions.
14 changes: 7 additions & 7 deletions packages/cantos/src/entrance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {NameList} from "@src/util-types.ts";
import {Story, UserNameList, UserStories, UserStory} from "@src/stories/stories.ts";

import {Domain, IStory, IStoryScript, UserDomain} from "@src/stories/interfaces.ts";
import {DomainDef, IStory, IStoryScript, UserDomain} from "@src/stories/interfaces.ts";
import {IStoryScripts} from "@src/stories/story-types.ts";
import {StoryOptions} from "@src/stories/story-options.ts";

Expand All @@ -18,12 +18,12 @@ function mapNameList<T extends NameList>(tell: T): Record<keyof T, StoryField> {
}, {} as Record<keyof T, StoryField>);
}

export function loadList<T extends NameList, DOMAINS extends Domain>(descriptions: T): UserNameList<T, DOMAINS> {
export function loadList<T extends NameList, DOMAINS extends DomainDef>(descriptions: T): UserNameList<T, DOMAINS> {
const result = mapNameList(descriptions);
return loadScriptRecord(result) as unknown as UserNameList<T, DOMAINS>;
}

export function loadDomain<T extends Domain>(domain: T): UserDomain<T> {
export function loadDomain<T extends DomainDef>(domain: T): UserDomain<T> {
return domain
}

Expand All @@ -33,15 +33,15 @@ export function loadDomain<T extends Domain>(domain: T): UserDomain<T> {
* @param opt - Optional parameters.
* @returns A ReadonlyDeep Act.
*/
export function loadScript<SCRIPT extends IStoryScript<DOMAINS>, DOMAINS extends Domain>(partialEntity: SCRIPT, opt?: StoryOptions): UserStory<SCRIPT, DOMAINS> {
export function loadScript<SCRIPT extends IStoryScript<DOMAINS>, DOMAINS extends DomainDef>(partialEntity: SCRIPT, opt?: StoryOptions): UserStory<SCRIPT, DOMAINS> {
const newStory = new Story(partialEntity, opt) as Story<DOMAINS>
for (const actKey in newStory.scenes) {
newStory.scenes[actKey] = instantiateStoriesRecursively(newStory.scenes[actKey]);
}
return newStory as UserStory<SCRIPT, DOMAINS>;
}

export function loadScriptRecord<SCRIPTS extends IStoryScripts<DOMAINS>, DOMAINS extends Domain>(actRecords: SCRIPTS): UserStories<SCRIPTS, DOMAINS> {
export function loadScriptRecord<SCRIPTS extends IStoryScripts<DOMAINS>, DOMAINS extends DomainDef>(actRecords: SCRIPTS): UserStories<SCRIPTS, DOMAINS> {
const stories = {} as any;

Object.keys(actRecords).forEach((actKey) => {
Expand All @@ -51,7 +51,7 @@ export function loadScriptRecord<SCRIPTS extends IStoryScripts<DOMAINS>, DOMAINS
return stories as UserStories<SCRIPTS, DOMAINS>;
}

function instantiateStoriesRecursively<T extends Story<DOMAIN>, DOMAIN extends Domain>(partialAct: IStory<DOMAIN>): T {
function instantiateStoriesRecursively<T extends Story<DOMAIN>, DOMAIN extends DomainDef>(partialAct: IStory<DOMAIN>): T {
const act = new Story(partialAct) as Story<DOMAIN>;
for (const actKey in act.scenes) {
act.scenes[actKey] = instantiateStoriesRecursively(act.scenes[actKey] as unknown as Story<DOMAIN>);
Expand All @@ -62,5 +62,5 @@ function instantiateStoriesRecursively<T extends Story<DOMAIN>, DOMAIN extends D
/**
* Types for Records.
*/
export type Scenes<DOMAIN extends Domain> = Record<string, IStory<DOMAIN>>;
export type Scenes<DOMAIN extends DomainDef> = Record<string, IStory<DOMAIN>>;
// Export entrance methods
11 changes: 5 additions & 6 deletions packages/cantos/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import {UserStory} from "@src/stories/stories.ts";
import {StoryScript} from "@src/stories/story-types.ts";
import {UserDomain, DomainObjectDef} from "@src/stories/interfaces.ts";
import {DomainDef, DomainObjectsDef, UserDomain} from "@src/stories/interfaces.ts";
// import and export entrance as Acts or A
import * as entrance from "./entrance.ts";
import {CommonScences, CommonTest, customizeCommonTests} from "./commons.ts";


// Re-export entrance methods
export * from "./entrance.ts";

// import and export entrance as Acts or A
import * as entrance from "./entrance.ts";
import { CommonScences, CommonTest, customizeCommonTests } from "./commons.ts";

// Export entrance as Cantos or C
export {entrance as Cantos, entrance as C};


// Export types for story
export type { StoryScript, UserStory, DomainObjectDef, UserDomain}
export type {StoryScript, UserStory, DomainObjectsDef, DomainDef, UserDomain}

// export Common Test helpers
export {CommonScences as CS, CommonTest as CT, CommonScences, CommonTest, customizeCommonTests}
Expand Down
24 changes: 12 additions & 12 deletions packages/cantos/src/stories/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {IStoryScripts} from "@src/stories/story-types.ts";
import {StoryStatus} from "@src/stories/status.ts";
import {StoryOptions} from "@src/stories/story-options.ts";

export interface Test<DOMAIN extends Domain = typeof EmptyDomain> {
export interface Test<DOMAIN extends DomainDef = typeof EmptyDomain> {
/**
* The type of tests.
*/
Expand Down Expand Up @@ -67,22 +67,22 @@ export interface DomainObjects<
}


export interface Domain<
export interface DomainDef<
TOBjects extends DomainObjects = typeof EmptyRecord,
TEvents extends DomainEvent = DomainEvent,
TCommands extends DomainCommand = DomainCommand,
> {
subdomains?: Record<string, Domain<any>>;
subdomains?: Record<string, DomainDef<any>>;
objects?: TOBjects;
aggregates?: Record<string, Aggregate<TOBjects>>;
events?: Record<string, TEvents>;
commands?: Record<string, TCommands>;
}

export type DomainObjectDef<T extends DomainObjects = DomainObjects> = T
export type DomainObjectsDef<T extends DomainObjects = DomainObjects> = T


export type UserDomain<T extends Domain> = T & Domain;
export type UserDomain<T extends DomainDef> = T & DomainDef;


export interface RuleExamples {
Expand All @@ -91,14 +91,14 @@ export interface RuleExamples {
}

export const EmptyRecord = {} as const;
export const EmptyDomain: Domain<any> = {} as const;
export const EmptyDomain: DomainDef<any> = {} as const;
// Dumb, or smart?, way to debug type inference: change never to a definite type, such as number, and see if `who` field inferre to that type, if it is, debug there, if not, move up the chain.
// Simplified type to extract entity keys from a domain
export type DomainEntityKeys<T extends Domain> = T['objects'] extends DomainObjects ? keyof T['objects']['entities'] : string;
export type DomainEntityKeys<T extends DomainDef> = T['objects'] extends DomainObjects ? keyof T['objects']['entities'] : string;

// Revised Who<T> type using the simplified utility type
export type Who<T extends Domain> = {
[K in keyof T]: T[K] extends Domain<any>
export type Who<T extends DomainDef> = {
[K in keyof T]: T[K] extends DomainDef<any>
? DomainEntityKeys<T[K]>
: string;
}[keyof T];
Expand All @@ -108,7 +108,7 @@ export type StringRecord = Record<string, string>;
/**
* Bare Act is the base structure for any entity or behavior without methods or nested entities.
*/
export interface IStoryScript<DOMAIN extends Domain = typeof EmptyRecord> {
export interface IStoryScript<DOMAIN extends DomainDef = typeof EmptyRecord> {
/**
* The actors of the story.
*
Expand All @@ -132,7 +132,7 @@ export interface IStoryScript<DOMAIN extends Domain = typeof EmptyRecord> {
* The order of the story among its sibling stories.
*/
order?: number;
domains?: DOMAIN;
domain?: DOMAIN;
/**
* Story-by-Story options that can override the global story options.
*/
Expand Down Expand Up @@ -192,7 +192,7 @@ export interface IStoryScript<DOMAIN extends Domain = typeof EmptyRecord> {

}

export interface IStory<DOMAIN extends Domain = typeof EmptyDomain> extends IStoryScript<DOMAIN> {
export interface IStory<DOMAIN extends DomainDef = typeof EmptyDomain> extends IStoryScript<DOMAIN> {
scenes: Scenes<DOMAIN>;
testId: () => string;
path: () => string;
Expand Down
8 changes: 4 additions & 4 deletions packages/cantos/src/stories/story-types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {Domain, EmptyRecord, IStoryScript} from "@src/stories/interfaces.ts";
import {DomainDef, EmptyRecord, IStoryScript} from "@src/stories/interfaces.ts";


/**
* Export type alises for easier use for user-defined data.
*/
type IStoryScripts<Cast extends Domain> = Record<string, IStoryScript<Cast>>;
type IStoryScripts<Cast extends DomainDef> = Record<string, IStoryScript<Cast>>;

/**
* Type for used defined input data for an Act.
*/
type StoryScript<Cast extends Domain = typeof EmptyRecord> = IStoryScript<Cast>;
type StoryScript<Cast extends DomainDef = typeof EmptyRecord> = IStoryScript<Cast>;


export type {StoryScript, IStoryScripts, Domain}
export type {StoryScript, IStoryScripts, DomainDef}


26 changes: 13 additions & 13 deletions packages/cantos/tests/fixtures/wizard-of-oz-story.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Domain, DomainObjectDef} from "@src/stories/interfaces.ts";
import {DomainDef, DomainObjectsDef} from "@src/stories/interfaces.ts";
import {StoryScript} from "@src/stories/story-types.ts";
import { loadScript} from "@src/entrance.ts";

Expand All @@ -11,7 +11,7 @@ const dorothyObjects = {
values: {
hasBrain: true
}
} satisfies DomainObjectDef;
} satisfies DomainObjectsDef;


const scareCrowObjects = {
Expand All @@ -24,9 +24,9 @@ const scareCrowObjects = {
values: {
hasBrain: false
}
} satisfies DomainObjectDef;
} satisfies DomainObjectsDef;

const scarecrowDomain: Domain<typeof scareCrowObjects> = {
const scarecrowDomain = {
objects: scareCrowObjects,
aggregates: {
scareCrowTheCharacter: {
Expand All @@ -39,7 +39,7 @@ const scarecrowDomain: Domain<typeof scareCrowObjects> = {
name: "sawDorothy"
}
}
} satisfies Domain<typeof scareCrowObjects>
} satisfies DomainDef<typeof scareCrowObjects>

const tinManObjects = {
entities: {
Expand All @@ -51,9 +51,9 @@ const tinManObjects = {
values: {
hasHeart: false
}
} satisfies DomainObjectDef;
} satisfies DomainObjectsDef;

const tinManDomain: Domain<typeof tinManObjects> = {
const tinManDomain: DomainDef<typeof tinManObjects> = {
objects: tinManObjects,
aggregates: {
tinManTheCharacter: {
Expand All @@ -66,7 +66,7 @@ const tinManDomain: Domain<typeof tinManObjects> = {
name: "sawDorothy"
}
}
} satisfies Domain<typeof tinManObjects>
} satisfies DomainDef<typeof tinManObjects>



Expand All @@ -91,11 +91,11 @@ const ozDomain = {
}
}

} satisfies Domain<typeof dorothyObjects>
} satisfies DomainDef<typeof dorothyObjects>

const dorothyMeetsTheScarecrow = {
story: "When Dorothy meets the Scarecrow",
domains: ozDomain,
domain: ozDomain,
scenes: {
DOROTHY_LOST_HER_WAY: {
story: "Dorothy lost her way",
Expand Down Expand Up @@ -158,7 +158,7 @@ const dorothyMeetsTheScarecrow = {

const dorothyMeetsTheLion = {
story: "When Dorothy meets the Lion",
domains: ozDomain,
domain: ozDomain,
scenes: {
THREE_IN_THE_FOREST: {
story: "The three were walking in the forest",
Expand Down Expand Up @@ -190,7 +190,7 @@ const dorothyMeetsTheLion = {

const dorothyMeetsScarecrowAndLion = {
story: "When Dorothy meets the Scarecrow and the Lion",
domains: ozDomain,
domain: ozDomain,
scenes: {
dorothyMeetsTheScarecrow,
dorothyMeetsTheLion,
Expand All @@ -199,7 +199,7 @@ const dorothyMeetsScarecrowAndLion = {

const dorothyMeetsHerCompanions = {
story: "Dorothy meets her companions",
domains: ozDomain,
domain: ozDomain,
scenes: {
dorothyMeetsScarecrowAndLion,
}
Expand Down
8 changes: 4 additions & 4 deletions packages/cantos/tests/tutorials/cantos-basics-mychat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
import {CommonScences, loadScript, StoryScript} from "@src/index";
import {describe, expect, it} from "vitest";
import {Domain, DomainObjectDef} from "@src/stories/interfaces.ts";
import {DomainDef, DomainObjectsDef} from "@src/stories/interfaces.ts";


/**
Expand Down Expand Up @@ -41,7 +41,7 @@ const domainsObjects = {
id: "InternetConnection",
},
},
} satisfies DomainObjectDef // <-- `satisfies` is a Typescript feature to let you have auto-complete when you write your stories with these cast members later.
} satisfies DomainObjectsDef // <-- `satisfies` is a Typescript feature to let you have auto-complete when you write your stories with these cast members later.

const myChatCastDomain = {
objects: domainsObjects,
Expand All @@ -67,7 +67,7 @@ const myChatCastDomain = {
actor: "User's Internet Provider"
},
}
} satisfies Domain<typeof domainsObjects>
} satisfies DomainDef<typeof domainsObjects>



Expand All @@ -84,7 +84,7 @@ const myChatStory = {
story: "The user uses MyChat to chat with the AI",

// We also provide the `cast` profiles we prepared earlier.
domains: myChatCastDomain, // <-- This let Cantos know which cast to use for your stories, so it can help you with auto-complete later.
domain: myChatCastDomain, // <-- This let Cantos know which cast to use for your stories, so it can help you with auto-complete later.

// The scenes that make up the story/movie/feature.
scenes: {
Expand Down
10 changes: 5 additions & 5 deletions packages/cantos/tests/units/story-teller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {GenreUserStory} from "@src/stories/story-kinds.ts";
import {StoryTag} from "@src/stories/story-options.ts";
import {TestKinds} from "@src/stories/test-kinds.ts";
import { CommonScences, CommonTest} from "@src/index";
import {Domain, DomainObjectDef} from "@src/stories/interfaces.ts";
import {DomainDef, DomainObjectsDef} from "@src/stories/interfaces.ts";

const storyTellDomainObjects = {
entities: {
Expand All @@ -19,7 +19,7 @@ const storyTellDomainObjects = {
id: "User",
},
}
} satisfies DomainObjectDef
} satisfies DomainObjectsDef

const storyTellerDomain = {
objects: storyTellDomainObjects,
Expand All @@ -34,11 +34,11 @@ const storyTellerDomain = {
root: "User"
}
}
} satisfies Domain<typeof storyTellDomainObjects>
} satisfies DomainDef<typeof storyTellDomainObjects>

const storyTellerStoryWho = {
who: ["Who"],
domains: storyTellerDomain,
domain: storyTellerDomain,
story: CommonScences.SHOULD_WORK.story,
scenes: {
tellWhoStory: {
Expand Down Expand Up @@ -97,7 +97,7 @@ const storyTellerStoryTags = {

const storyTellerScript = {
story: "Story Teller",
domains: storyTellerDomain,
domain: storyTellerDomain,
scenes: {
who: storyTellerStoryWho,
tags: storyTellerStoryTags,
Expand Down

0 comments on commit 827a9e4

Please sign in to comment.