Skip to content

Commit

Permalink
Make it so seeds are planted in different locations in garden based o…
Browse files Browse the repository at this point in the history
…n location.

Part of #3.
  • Loading branch information
jkomoros committed Jun 21, 2023
1 parent 16a02b1 commit fa360f6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
26 changes: 21 additions & 5 deletions src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ import {
Seed
} from './seed.js';

import {
packSeedReferenceID,
unpackSeedReferenceID
} from './reference.js';

export class Garden {
_env : Environment;
_seeds : {[id : SeedReferenceID] : Seed};
_seeds : {
[location : SeedPacketLocation]: {
[id : SeedReferenceID] : Seed
}
};
_location? : SeedPacketLocation;

constructor(environment : EnvironmentData) {
Expand All @@ -34,19 +43,26 @@ export class Garden {
}

seed(id : SeedReferenceID = '') : Seed {
const seed = this._seeds[id];
const unpacked = unpackSeedReferenceID(id, this.location);
const collection = this._seeds[unpacked.location];
if (!collection) throw new Error(`No seed with ID ${id}`);
const seed = collection[unpacked.id];
if (!seed) throw new Error(`No seed with ID ${id}`);
return seed;
}

plantSeed(id : SeedReferenceID, data : SeedData) {
this._seeds[id] = new Seed(this, id, data);
const unpacked = unpackSeedReferenceID(id, this.location);
if (this._seeds[unpacked.location] == undefined) {
this._seeds[unpacked.location] = {};
}
this._seeds[unpacked.location][unpacked.id] = new Seed(this, id, data);
}

plantSeedPacket(location: SeedPacketLocation, packet: SeedPacket) {
if (!this._location) this._location = location;
//TODO: combine IDs with the URL they came from so no collisions
for (const [id, seed] of Object.entries(packet.seeds)) {
for (const [localID, seed] of Object.entries(packet.seeds)) {
const id = packSeedReferenceID(location, localID);
this.plantSeed(id, seed);
}
}
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions test/base/b_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 0,
"seeds": {
"": {
"type": "log",
"value": "test-other hello world"
}
}
}
27 changes: 21 additions & 6 deletions test/base/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import {
import assert from 'assert';

import {
readFileSync
readFileSync,
readdirSync
} from 'fs';

const TEST_PACKET_LOCATION = 'test/base/test.json';
import * as path from 'path';

const TEST_PACKETS_LOCATION = 'test/base/';

const loadTestGarden = () : Garden => {
const env : Required<EnvironmentData> = {
Expand All @@ -28,10 +31,14 @@ const loadTestGarden = () : Garden => {
verbose: false
};
const garden = new Garden(env);
const data = readFileSync(TEST_PACKET_LOCATION).toString();
const json = JSON.parse(data);
const packet = seedPacket.parse(json);
garden.plantSeedPacket(TEST_PACKET_LOCATION, packet);
for (const file of readdirSync(TEST_PACKETS_LOCATION)) {
if (path.extname(file) != '.json') continue;
const filename = path.join(TEST_PACKETS_LOCATION, file);
const data = readFileSync(filename).toString();
const json = JSON.parse(data);
const packet = seedPacket.parse(json);
garden.plantSeedPacket(filename, packet);
}
return garden;
};

Expand Down Expand Up @@ -101,4 +108,12 @@ describe('Garden smoke test', () => {
assert.deepEqual(result, golden);
});

it('handles a seed from another file', async() => {
const garden = loadTestGarden();
const seed = garden.seed('test/base/b_test.json#');
const result = await seed.grow();
const golden = 'test-other hello world';
assert.deepStrictEqual(result, golden);
});

});

0 comments on commit fa360f6

Please sign in to comment.