@@ -4,7 +4,7 @@ const HDSLib = require('../js');
44const { createUserAndPermissions } = require ( './test-utils/pryvService' ) ;
55
66describe ( '[TKSX] toolKit Stream Auto Create' , function ( ) {
7- this . timeout ( 5000 ) ;
7+ this . timeout ( 10000 ) ;
88 before ( async ( ) => {
99 await HDSLib . initHDSModel ( ) ;
1010 } ) ;
@@ -27,4 +27,75 @@ describe('[TKSX] toolKit Stream Auto Create', function () {
2727 const createdStream2 = await streamsAutoCreate . ensureExistsForItems ( itemKeys ) ;
2828 assert . equal ( createdStream2 . length , 0 , 'Should not recreate existing streams' ) ;
2929 } ) ;
30+
31+ it ( '[TKSB] attachToConnection reuses existing instance' , async ( ) => {
32+ const permissionsManager = [ { streamId : '*' , level : 'manage' } ] ;
33+ const user = await createUserAndPermissions ( null , permissionsManager , [ ] , 'tk-test TKSB' ) ;
34+ const connection = new HDSLib . pryv . Connection ( user . appApiEndpoint ) ;
35+
36+ const streamsAutoCreate1 = HDSLib . toolkit . StreamsAutoCreate . attachToConnection ( connection ) ;
37+ const streamsAutoCreate2 = HDSLib . toolkit . StreamsAutoCreate . attachToConnection ( connection ) ;
38+ assert . strictEqual ( streamsAutoCreate1 , streamsAutoCreate2 , 'Should reuse existing instance' ) ;
39+ } ) ;
40+
41+ it ( '[TKSC] addStreamStructure adds streams to known list' , async ( ) => {
42+ const permissionsManager = [ { streamId : '*' , level : 'manage' } ] ;
43+ const user = await createUserAndPermissions ( null , permissionsManager , [ ] , 'tk-test TKSC' ) ;
44+ const connection = new HDSLib . pryv . Connection ( user . appApiEndpoint ) ;
45+ const streamsAutoCreate = HDSLib . toolkit . StreamsAutoCreate . attachToConnection ( connection ) ;
46+
47+ // Add stream structure
48+ const streamStructure = [
49+ { id : 'existing-parent' , name : 'Existing Parent' , children : [ { id : 'existing-child' , name : 'Existing Child' } ] }
50+ ] ;
51+ streamsAutoCreate . addStreamStructure ( streamStructure ) ;
52+
53+ const knownIds = streamsAutoCreate . knowStreamIds ( ) ;
54+ assert . ok ( knownIds . includes ( 'existing-parent' ) ) ;
55+ assert . ok ( knownIds . includes ( 'existing-child' ) ) ;
56+ } ) ;
57+
58+ it ( '[TKSD] addStreamStructure handles null input' , async ( ) => {
59+ const permissionsManager = [ { streamId : '*' , level : 'manage' } ] ;
60+ const user = await createUserAndPermissions ( null , permissionsManager , [ ] , 'tk-test TKSD' ) ;
61+ const connection = new HDSLib . pryv . Connection ( user . appApiEndpoint ) ;
62+ const streamsAutoCreate = HDSLib . toolkit . StreamsAutoCreate . attachToConnection ( connection ) ;
63+
64+ // Should not throw
65+ streamsAutoCreate . addStreamStructure ( null ) ;
66+ streamsAutoCreate . addStreamStructure ( undefined ) ;
67+ } ) ;
68+
69+ it ( '[TKSE] ensureExistsForItems handles Set input' , async ( ) => {
70+ const permissionsManager = [ { streamId : '*' , level : 'manage' } ] ;
71+ const user = await createUserAndPermissions ( null , permissionsManager , [ ] , 'tk-test TKSE' ) ;
72+ const connection = new HDSLib . pryv . Connection ( user . appApiEndpoint ) ;
73+ const streamsAutoCreate = HDSLib . toolkit . StreamsAutoCreate . attachToConnection ( connection ) ;
74+
75+ const itemKeys = new Set ( [ 'profile-surname' ] ) ;
76+
77+ const createdStreams = await streamsAutoCreate . ensureExistsForItems ( itemKeys ) ;
78+ assert . ok ( createdStreams . length > 0 ) ;
79+ } ) ;
80+
81+ it ( '[TKSF] ensureExistsForItems with item-already-exists error continues normally' , async ( ) => {
82+ const permissionsManager = [ { streamId : '*' , level : 'manage' } ] ;
83+ const user = await createUserAndPermissions ( null , permissionsManager , [ ] , 'tk-test TKSF' ) ;
84+ const connection = new HDSLib . pryv . Connection ( user . appApiEndpoint ) ;
85+ const streamsAutoCreate = HDSLib . toolkit . StreamsAutoCreate . attachToConnection ( connection ) ;
86+
87+ const itemKeys = [ 'profile-date-of-birth' ] ;
88+
89+ // Create streams first
90+ await streamsAutoCreate . ensureExistsForItems ( itemKeys ) ;
91+
92+ // Create a fresh instance that doesn't know about existing streams
93+ delete connection . streamsAutoCreate ;
94+ const freshAutoCreate = HDSLib . toolkit . StreamsAutoCreate . attachToConnection ( connection ) ;
95+
96+ // Should handle item-already-exists gracefully
97+ const result = await freshAutoCreate . ensureExistsForItems ( itemKeys ) ;
98+ // Since streams exist but freshAutoCreate doesn't know, API returns item-already-exists which is handled
99+ assert . ok ( Array . isArray ( result ) ) ;
100+ } ) ;
30101} ) ;
0 commit comments