Skip to content

Commit

Permalink
refactor(tree): remove schema builder from forest test suite (microso…
Browse files Browse the repository at this point in the history
…ft#22096)

- removes schema builder usage from the forest test suite
- changes the namespace of the test simple tree schemas to match the
json domain
  • Loading branch information
jenn-le authored Aug 7, 2024
1 parent f3c26fa commit 0a3a965
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 54 deletions.
89 changes: 38 additions & 51 deletions packages/dds/tree/src/test/forestTestSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,34 @@ import {
} from "../core/index.js";
import {
cursorToJsonObject,
jsonRoot,
jsonSchema,
leaf,
singleJsonCursor,
typedJsonCursor,
} from "../domains/index.js";
import { typeboxValidator } from "../external-utilities/index.js";
import {
FieldKinds,
FlexFieldSchema,
SchemaBuilderBase,
cursorForJsonableTreeNode,
defaultSchemaPolicy,
intoStoredSchema,
isNeverField,
jsonableTreeFromCursor,
} from "../feature-libraries/index.js";
import {
type IdAllocator,
type JsonCompatible,
brand,
idAllocatorFromMaxId,
mapIterable,
} from "../util/index.js";

import { testGeneralPurposeTreeCursor, testTreeSchema } from "./cursorTestSuite.js";
import {
applyTestDelta,
expectEqualFieldPaths,
expectEqualPaths,
JsonArray,
jsonSequenceRootSchema,
testIdCompressor,
testRevisionTagCodec,
} from "./utils.js";
import { SchemaFactory, toFlexSchema } from "../simple-tree/index.js";

/**
* Configuration for the forest test suite.
Expand All @@ -84,11 +78,6 @@ export interface ForestTestConfiguration {
skipCursorErrorCheck?: true;
}

const jsonDocumentSchema = new SchemaBuilderBase(FieldKinds.sequence, {
scope: "jsonDocumentSchema",
libraries: [jsonSchema],
}).intoSchema(jsonRoot);

const buildId = { minor: 42 };
const buildId2 = { minor: 442 };
const detachId = { minor: 43 };
Expand Down Expand Up @@ -118,19 +107,14 @@ export function testForest(config: ForestTestConfiguration): void {
];
for (const [name, data] of testCases) {
it(name, () => {
const schema = new TreeStoredSchemaRepository();
const forest = factory(schema);
const schemaFactory = new SchemaFactory("forest test suite");

const rootFieldSchema = FlexFieldSchema.create(FieldKinds.optional, jsonRoot).stored;
schema.apply({
nodeSchema: new Map(
mapIterable(jsonSchema.nodeSchema.entries(), ([k, v]) => [k, v.stored]),
),
rootFieldSchema,
});
const rootSchema = schemaFactory.optional([JsonArray]);
const schema = new TreeStoredSchemaRepository(
intoStoredSchema(toFlexSchema(rootSchema)),
);

// Check schema is actually valid. If we forgot to add some required types this would fail.
assert(!isNeverField(defaultSchemaPolicy, schema, rootFieldSchema));
const forest = factory(schema);

initializeForest(
forest,
Expand All @@ -152,7 +136,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("cursor use", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonSequenceRootSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);
initializeForest(
forest,
Expand Down Expand Up @@ -190,7 +174,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("isEmpty: rootFieldKey", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);
assert(forest.isEmpty);
initializeForest(forest, [singleJsonCursor([])], testRevisionTagCodec, testIdCompressor);
Expand All @@ -199,7 +183,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("isEmpty: other root", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);
assert(forest.isEmpty);

Expand All @@ -225,7 +209,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("tryMoveCursorToNode", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);

initializeForest(
Expand Down Expand Up @@ -268,7 +252,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("tryMoveCursorToField", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);

initializeForest(
Expand Down Expand Up @@ -313,7 +297,7 @@ export function testForest(config: ForestTestConfiguration): void {
describe("moveCursorToPath", () => {
it("moves cursor to specified path.", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);
initializeForest(
forest,
Expand All @@ -336,7 +320,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("getCursorAboveDetachedFields", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);
initializeForest(
forest,
Expand All @@ -357,7 +341,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("anchors creation and use", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);
initializeForest(
forest,
Expand Down Expand Up @@ -427,7 +411,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("using an anchor that went away returns NotFound", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);

initializeForest(
Expand Down Expand Up @@ -462,7 +446,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("can destroy detached fields", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);
const content: JsonCompatible[] = [1, 2];
initializeForest(
Expand Down Expand Up @@ -519,7 +503,9 @@ export function testForest(config: ForestTestConfiguration): void {
});

it("primitive nodes", () => {
const schema = new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema));
const schema = new TreeStoredSchemaRepository(
intoStoredSchema(toFlexSchema(JsonArray)),
);
const forest = factory(schema);
const content: JsonCompatible[] = [1, true, "test"];
initializeForest(
Expand All @@ -542,7 +528,9 @@ export function testForest(config: ForestTestConfiguration): void {
});

it("multiple fields", () => {
const schema = new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema));
const schema = new TreeStoredSchemaRepository(
intoStoredSchema(toFlexSchema(JsonArray)),
);
const forest = factory(schema);
initializeForest(
forest,
Expand All @@ -560,7 +548,9 @@ export function testForest(config: ForestTestConfiguration): void {
});

it("with anchors", () => {
const schema = new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema));
const schema = new TreeStoredSchemaRepository(
intoStoredSchema(toFlexSchema(JsonArray)),
);
const forest = factory(schema);
initializeForest(
forest,
Expand Down Expand Up @@ -766,7 +756,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("remove", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);
const content: JsonCompatible[] = [1, 2];
initializeForest(
Expand All @@ -793,7 +783,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("a skip", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);
const content: JsonCompatible[] = [1, 2];
initializeForest(
Expand Down Expand Up @@ -825,7 +815,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("insert", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);
const content: JsonCompatible[] = [1, 2];
initializeForest(
Expand Down Expand Up @@ -855,7 +845,7 @@ export function testForest(config: ForestTestConfiguration): void {

it("move-out under transient node", () => {
const forest = factory(
new TreeStoredSchemaRepository(intoStoredSchema(jsonDocumentSchema)),
new TreeStoredSchemaRepository(intoStoredSchema(toFlexSchema(JsonArray))),
);

const moveId = { minor: 1 };
Expand Down Expand Up @@ -1142,22 +1132,19 @@ export function testForest(config: ForestTestConfiguration): void {
assert.deepEqual(actual, expected);
});
it("when moving the last node in the field", () => {
const builder = new SchemaBuilderBase(FieldKinds.sequence, {
scope: "moving",
libraries: [leaf.library],
});
const root = builder.object("root", {
x: leaf.number,
y: leaf.number,
const schemaFactory = new SchemaFactory("moving");
const NodeSchema = schemaFactory.object("root", {
x: schemaFactory.number,
y: schemaFactory.number,
});
const schema = builder.intoSchema(FlexFieldSchema.create(FieldKinds.optional, [root]));
const schema = toFlexSchema(schemaFactory.array(NodeSchema));

const forest = factory(new TreeStoredSchemaRepository(intoStoredSchema(schema)));
initializeForest(
forest,
[
typedJsonCursor({
[typedJsonCursor.type]: root,
[typedJsonCursor.type]: NodeSchema.identifier,
x: [2],
y: [1],
}),
Expand All @@ -1184,7 +1171,7 @@ export function testForest(config: ForestTestConfiguration): void {
const delta: DeltaFieldMap = new Map([[rootFieldKey, { local: [modify] }]]);
applyTestDelta(delta, forest);
const expectedCursor = typedJsonCursor({
[typedJsonCursor.type]: root,
[typedJsonCursor.type]: NodeSchema.identifier,
y: [1, 2],
});
const expected: JsonableTree[] = [jsonableTreeFromCursor(expectedCursor)];
Expand Down
4 changes: 2 additions & 2 deletions packages/dds/tree/src/test/shared-tree/sharedTree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ describe("SharedTree", () => {
const snapshot = sharedTree.contentSnapshot();
assert.deepEqual(snapshot.tree, [
{
type: `array`,
type: `com.fluidframework.json.array`,
fields: {
[EmptyKey]: [{ type: "com.fluidframework.leaf.string", value: "x" }],
},
Expand Down Expand Up @@ -2023,7 +2023,7 @@ describe("SharedTree", () => {
const changesSummary = JSON.parse(editManagerSummaryBlob.content as string);
const encodedTreeData = changesSummary.trunk[0].change[1].data.builds.trees;
const expectedUncompressedTreeData = [
"array",
"com.fluidframework.json.array",
false,
[
EmptyKey,
Expand Down
2 changes: 1 addition & 1 deletion packages/dds/tree/src/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ export function flexTreeFromForest<TRoot extends FlexFieldSchema>(
return view.flexTree;
}

const sf = new SchemaFactory(undefined);
const sf = new SchemaFactory("com.fluidframework.json");

export const NumberArray = sf.array("array", sf.number);
export const StringArray = sf.array("array", sf.string);
Expand Down

0 comments on commit 0a3a965

Please sign in to comment.