@@ -64,6 +64,86 @@ module PlaceOS::Model
6464 updated.should_not eq playlist.updated_at
6565 end
6666
67+ it " creates a plugin item" do
68+ plugin = Generator .signage_plugin.save!
69+ item = Generator .plugin_item(plugin: plugin)
70+ item.save!
71+
72+ found = Playlist ::Item .find(item.id.as(String ))
73+ found.media_type.should eq Playlist ::Item ::MediaType ::Plugin
74+ found.plugin_id.should eq plugin.id
75+ end
76+
77+ it " requires a plugin_id for plugin items" do
78+ item = Generator .item
79+ item.media_type = Playlist ::Item ::MediaType ::Plugin
80+ item.media_uri = nil
81+ item.plugin_id = nil
82+ item.save.should eq false
83+ item.errors.any? { |e | e.field == :plugin_id }.should eq true
84+ end
85+
86+ it " validates plugin_params keys exist in plugin params properties" do
87+ plugin = Generator .signage_plugin(
88+ params: {
89+ " type" => JSON ::Any .new(" object" ),
90+ " properties" => JSON ::Any .new({
91+ " play_at_period" => JSON ::Any .new({" type" => JSON ::Any .new(" integer" )} of String => JSON ::Any ),
92+ } of String => JSON ::Any ),
93+ },
94+ ).save!
95+
96+ item = Generator .plugin_item(
97+ plugin: plugin,
98+ plugin_params: {" bad_key" => JSON ::Any .new(5 _i64 )},
99+ )
100+ item.save.should eq false
101+ item.errors.any? { |e | e.field == :plugin_params }.should eq true
102+ end
103+
104+ it " validates required params are satisfied by defaults merged with plugin_params" do
105+ plugin = Generator .signage_plugin(
106+ params: {
107+ " type" => JSON ::Any .new(" object" ),
108+ " properties" => JSON ::Any .new({
109+ " play_at_period" => JSON ::Any .new({" type" => JSON ::Any .new(" integer" )} of String => JSON ::Any ),
110+ " color" => JSON ::Any .new({" type" => JSON ::Any .new(" string" )} of String => JSON ::Any ),
111+ } of String => JSON ::Any ),
112+ " required" => JSON ::Any .new([JSON ::Any .new(" play_at_period" ), JSON ::Any .new(" color" )]),
113+ },
114+ defaults: {" play_at_period" => JSON ::Any .new(10 _i64 )},
115+ ).save!
116+
117+ # missing "color" which is required and has no default
118+ item = Generator .plugin_item(
119+ plugin: plugin,
120+ plugin_params: {} of String => JSON ::Any ,
121+ )
122+ item.save.should eq false
123+ item.errors.any? { |e | e.field == :plugin_params && e.message.to_s.includes?(" color" ) }.should eq true
124+ end
125+
126+ it " allows plugin_params when defaults cover required params" do
127+ plugin = Generator .signage_plugin(
128+ params: {
129+ " type" => JSON ::Any .new(" object" ),
130+ " properties" => JSON ::Any .new({
131+ " play_at_period" => JSON ::Any .new({" type" => JSON ::Any .new(" integer" )} of String => JSON ::Any ),
132+ " color" => JSON ::Any .new({" type" => JSON ::Any .new(" string" )} of String => JSON ::Any ),
133+ } of String => JSON ::Any ),
134+ " required" => JSON ::Any .new([JSON ::Any .new(" play_at_period" ), JSON ::Any .new(" color" )]),
135+ },
136+ defaults: {" play_at_period" => JSON ::Any .new(10 _i64 )},
137+ ).save!
138+
139+ # "color" provided in plugin_params, "play_at_period" covered by defaults
140+ item = Generator .plugin_item(
141+ plugin: plugin,
142+ plugin_params: {" color" => JSON ::Any .new(" red" )},
143+ )
144+ item.save.should eq true
145+ end
146+
67147 it " updates playlists when an item is modified" do
68148 revision = Generator .revision
69149
0 commit comments