UNPKG

4.58 kBtext/coffeescriptView Raw
1path = require 'path'
2
3should = require 'should'
4mockery = require 'mockery'
5_ = require 'lodash'
6logger = require 'torch'
7
8core = require '../lib/core'
9findProjectRoot = require '../lib/findProjectRoot'
10
11sampleDir = path.join __dirname, '../sample'
12sampleProjDir = path.join sampleDir, 'project1'
13sample = require path.join sampleDir, 'sample'
14
15describe 'core.init', ->
16 beforeEach ->
17 process.chdir sampleProjDir
18
19 @retriever = _.clone require '../lib/retriever'
20 @retriever.projectRoot = findProjectRoot(process.cwd())
21
22 mockery.enable
23 warnOnReplace: false,
24 warnOnUnregistered: false
25
26 mockery.registerMock @retriever.rel('node_modules', 'axiom-base'), {
27 services:
28 runtime: (args, next) ->
29 next null, {message: 'axiom-base'}
30 }
31 prefix = @retriever.rel 'node_modules', 'axiom-sample'
32 mockery.registerMock @retriever.rel('node_modules', 'axiom-sample'), sample
33
34 afterEach ->
35 core.reset()
36 mockery.disable()
37
38 after ->
39 core.reset()
40
41 it 'should load axiom-base', (done) ->
42 core.init()
43
44 core.request 'base.runtime', {}, (err, result) ->
45 should.not.exist err
46 should.exist result
47 result.should.eql {message: 'axiom-base'}
48 done()
49
50 it 'should dynamically load a module based on name', (done) ->
51 data = {greeting: 'hello!'}
52 config =
53 modules: ['sample']
54 core.init config
55
56 core.request 'sample.echo', data, (err, result) ->
57 should.not.exist err
58 should.exist result
59 result.should.eql data
60 done()
61
62 it 'should not init a module that is blacklisted', (done) ->
63 config =
64 blacklist: ['sample']
65 modules: ['sample']
66 core.init config
67
68 core.request 'sample.echo', {greeting: 'hello!'}, (err, result) ->
69 should.exist err
70 err.message.should.eql "No responders for request: 'sample.echo'"
71
72 should.not.exist result
73
74 done()
75
76 it "should load a global 'axiom' file from the project root", (done) ->
77 internal = require '../lib/core/internal'
78 axiomFile = require path.join(sampleProjDir, 'axiom')
79 should.exist axiomFile
80 core.init @retriever
81 internal.config.should.include axiomFile
82 done()
83
84 it "should assume an 'axiom_configs' folder containing config overrides", (done) ->
85 core.init {modules: ['sample']}, @retriever
86
87 # Given an extension with a service and corresponding config entry
88 defaultSampleConfig = sample.config.whatsMyContext
89 should.exist defaultSampleConfig
90
91 # And a config override in the local project
92 overrideConfigPath = path.join sampleProjDir, 'axiom_configs', 'sample'
93 overrideConfig = require(overrideConfigPath).whatsMyContext
94 should.exist overrideConfig
95
96 expectedConfig = _.merge {}, defaultSampleConfig, overrideConfig
97
98 # When the service is called
99 core.request 'sample.whatsMyContext', {}, (err, config) ->
100 should.not.exist err
101
102 # Then the resulting @config should be the default merged with the override
103 should.exist config
104 config.should.eql expectedConfig
105
106 done()
107
108 it "'retriever' in 'util' should be default instance", (done) ->
109 defaultRetriever = @retriever
110
111 # Given a service
112 server =
113 services:
114 "run/prepare": (args, fin) ->
115
116 # Then the retriever should include the default 'retriever'
117 should.exist @util
118 @util.should.include defaultRetriever
119 fin()
120
121 # When core is initialized without injecting a 'retriever'
122 core.init()
123 core.load "server", server
124
125 # And the service is called
126 core.request "server.run/prepare", {}, (err, result) ->
127 should.not.exist err
128 done()
129
130 it "should expose an injected 'retriever' in 'util'", (done) ->
131 defaultRetriever = @retriever
132
133 # Given a mock test 'retriever'
134 mockRetriever =
135 retrieve: (name...) -> {}
136 retrieveExtension: (name...) -> {}
137
138 # And a test service
139 server =
140 services:
141 "run/prepare": (args, fin) ->
142 should.exist @util
143
144 # Then @util should include the mock retriever
145 @util.should.include mockRetriever
146
147 # And not the default retriever
148 @util.should.not.include defaultRetriever
149
150 fin()
151
152 # When core is initialized with the mock retriever
153 core.init {}, mockRetriever
154
155 # And the service is loaded
156 core.load "server", server
157
158 # And the service is called
159 core.request "server.run/prepare", {}, (err, result) ->
160
161 # It should return without its assertions failing
162 should.not.exist err
163 done()